## Issue Closes #5531 ## Change `DefaultServerSentEventParser` applied `content.trim()` to each `data:` field value, removing all leading and trailing whitespace. The WHATWG HTML Living Standard (Server-sent events, "Interpreting an event stream") requires removing only a single leading U+0020 SPACE when present; further leading whitespace and all trailing whitespace must be preserved. The old behavior corrupted values with multiple leading spaces, trailing spaces, or indentation, which matters for LLM token streaming, the module's primary use. This PR replaces `trim()` with a single-leading-space strip on the `data:` branch only; `event:` handling is left unchanged to keep the change minimal. Added unit tests for preserving additional leading whitespace, preserving trailing whitespace, and leaving values with no leading space unchanged. Existing parameterized and multi-line tests remain green (each line still loses one leading space). Could a maintainer confirm whether the original `trim()` was intentional normalization rather than a bug? ## General checklist - [x] There are no breaking changes (API, behaviour) - [x] I have added unit and/or integration tests for my change - [x] The tests cover both positive and negative cases - [x] I have manually run all the unit and integration tests in the module I have added/changed, and they are all green - [ ] I have manually run all the unit and integration tests in the [core](https://github.com/langchain4j/langchain4j/tree/main/langchain4j-core) and [main](https://github.com/langchain4j/langchain4j/tree/main/langchain4j) modules, and they are all green - [ ] I have added/updated the [documentation](https://github.com/langchain4j/langchain4j/tree/main/docs/docs) - [ ] I have added an example in the [examples repo](https://github.com/langchain4j/langchain4j-examples) (only for "big" features) - [ ] I have added/updated [Spring Boot starter(s)](https://github.com/langchain4j/langchain4j-spring) (if applicable) <!-- Not applicable: no new maven module, no embedding store integration changes. --> |
||
|---|---|---|
| .devcontainer | ||
| .github | ||
| .idea | ||
| .mvn | ||
| code-execution-engines | ||
| docs | ||
| document-loaders | ||
| document-parsers | ||
| document-transformers/langchain4j-document-transformer-jsoup | ||
| embedding-store-filter-parsers/langchain4j-embedding-store-filter-parser-sql | ||
| embeddings | ||
| experimental | ||
| http-clients | ||
| integration-tests | ||
| internal | ||
| langchain4j | ||
| langchain4j-agentic | ||
| langchain4j-agentic-a2a | ||
| langchain4j-agentic-mcp | ||
| langchain4j-agentic-patterns | ||
| langchain4j-anthropic | ||
| langchain4j-azure-ai-search | ||
| langchain4j-azure-cosmos-mongo-vcore | ||
| langchain4j-azure-cosmos-nosql | ||
| langchain4j-azure-open-ai | ||
| langchain4j-bedrock | ||
| langchain4j-bom | ||
| langchain4j-cassandra | ||
| langchain4j-chroma | ||
| langchain4j-cohere | ||
| langchain4j-coherence | ||
| langchain4j-core | ||
| langchain4j-couchbase | ||
| langchain4j-easy-rag | ||
| langchain4j-elasticsearch | ||
| langchain4j-github-models | ||
| langchain4j-google-ai-gemini | ||
| langchain4j-google-genai | ||
| langchain4j-gpu-llama3 | ||
| langchain4j-guardrails | ||
| langchain4j-hibernate | ||
| langchain4j-http-client | ||
| langchain4j-hugging-face | ||
| langchain4j-infinispan | ||
| langchain4j-jina | ||
| langchain4j-jlama | ||
| langchain4j-kotlin | ||
| langchain4j-local-ai | ||
| langchain4j-mariadb | ||
| langchain4j-mcp | ||
| langchain4j-mcp-docker | ||
| langchain4j-micrometer-metrics | ||
| langchain4j-milvus | ||
| langchain4j-milvus-v2 | ||
| langchain4j-mistral-ai | ||
| langchain4j-mongodb-atlas | ||
| langchain4j-nomic | ||
| langchain4j-observation | ||
| langchain4j-ollama | ||
| langchain4j-onnx-scoring | ||
| langchain4j-open-ai | ||
| langchain4j-open-ai-official | ||
| langchain4j-opensearch | ||
| langchain4j-oracle | ||
| langchain4j-ovh-ai | ||
| langchain4j-parent | ||
| langchain4j-pgvector | ||
| langchain4j-pinecone | ||
| langchain4j-qdrant | ||
| langchain4j-skills | ||
| langchain4j-tablestore | ||
| langchain4j-test | ||
| langchain4j-vertex-ai | ||
| langchain4j-vertex-ai-anthropic | ||
| langchain4j-vertex-ai-gemini | ||
| langchain4j-vespa | ||
| langchain4j-voyage-ai | ||
| langchain4j-watsonx | ||
| langchain4j-weaviate | ||
| langchain4j-workers-ai | ||
| web-search-engines | ||
| .editorconfig | ||
| .gitattributes | ||
| .gitignore | ||
| .prettierrc | ||
| CODE_OF_CONDUCT.md | ||
| CONTRIBUTING.md | ||
| LICENSE | ||
| LOGO_LICENSE.md | ||
| Makefile | ||
| README.md | ||
| SECURITY.md | ||
| check-split-packages.sh | ||
| detekt.yml | ||
| mvnw | ||
| mvnw.cmd | ||
| pom.xml | ||
| renovate.json5 | ||
README.md
LangChain4j: idiomatic, open-source Java library for building LLM-powered applications on the JVM
Introduction
Welcome!
The goal of LangChain4j is to simplify integrating LLMs into Java applications.
Here's how:
- Unified APIs: LLM providers (like OpenAI or Google Vertex AI) and embedding (vector) stores (such as Pinecone or Milvus) use proprietary APIs. LangChain4j offers a unified API to avoid the need for learning and implementing specific APIs for each of them. To experiment with different LLMs or embedding stores, you can easily switch between them without the need to rewrite your code. LangChain4j currently supports 20+ popular LLM providers and 30+ embedding stores.
- Comprehensive Toolbox: Since early 2023, the community has been building numerous LLM-powered applications, identifying common abstractions, patterns, and techniques. LangChain4j has refined these into practical code. Our toolbox includes tools ranging from low-level prompt templating, chat memory management, and function calling to high-level patterns like Agents and RAG. For each abstraction, we provide an interface along with multiple ready-to-use implementations based on common techniques. Whether you're building a chatbot or developing a RAG with a complete pipeline from data ingestion to retrieval, LangChain4j offers a wide variety of options.
- Numerous Examples: These examples showcase how to begin creating various LLM-powered applications, providing inspiration and enabling you to start building quickly.
LangChain4j began development in early 2023 amid the ChatGPT hype. We noticed a lack of Java counterparts to the numerous Python and JavaScript LLM libraries and frameworks, and we had to fix that!
Despite the name, LangChain4j is not a Java port of LangChain (Python) — it is built for Java, not ported to it. It is an idiomatic Java library designed from the ground up around Java conventions: type safety, POJOs, annotations, interfaces, dependency injection, fluent APIs, and first-class integrations with Quarkus, Spring Boot, Helidon, and Micronaut. Its API, internals, and release cycle are independent of the Python LangChain project.
We actively monitor community developments, aiming to quickly incorporate new techniques and integrations, ensuring you stay up-to-date. The library is under active development. While some features are still being worked on, the core functionality is in place, allowing you to start building LLM-powered apps now!
Documentation
Documentation can be found here.
The documentation chatbot (experimental) can be found here.
Getting Started
Getting started guide can be found here.
Code Examples
Please see examples of how LangChain4j can be used in langchain4j-examples repo:
- Examples in plain Java
- Examples with Quarkus (uses quarkus-langchain4j dependency)
- Example with Spring Boot
- Examples with Helidon (uses io.helidon.integrations.langchain4j dependency)
- Examples with Micronaut (uses micronaut-langchain4j dependency)
Useful Materials
Useful materials can be found here.
Get Help
Please use Discord or GitHub discussions to get help.
Request Features
Please let us know what features you need by opening an issue.
Contribute
Contribution guidelines can be found here.