项目文件夹
## Issue Closes #6363 ## Change `AgenticScope.agentInvocations()` documents its return value as "an unmodifiable list of all agent invocations", but `DefaultAgenticScope` returned the internal list field, so `agenticScope().agentInvocations().clear()` silently erased the scope's execution history and `add(...)` could insert entries that no agent produced. ```java return Collections.unmodifiableList(agentInvocations); ``` `AgentsRegistry.allAgents()` carries the same contract and already enforces it this way, so this is the pattern the module already uses rather than a new one. The distinction is deliberate elsewhere in this interface: `state()` documents "a live view" and "the mutable state map", and the two filtered overloads `agentInvocations(String)` and `agentInvocations(Class<?>)` are already unmodifiable because they collect with `Stream.toList()`. This one method was the exception. Reads are unaffected: the wrapper is a view over the same list, so invocations recorded after the call are still visible through it. Nothing inside the module reads through this accessor, every writer uses the field directly, and no caller anywhere in the repository mutates the returned list, so the only behaviour that changes is that a mutation attempt now throws `UnsupportedOperationException` instead of corrupting the history. ### Tests - `AgentInvocationsTest.should_not_allow_modifying_agent_invocations` (new) records one invocation, asserts it is readable, asserts `clear()` throws `UnsupportedOperationException`, and asserts the history is still intact afterwards. It fails on unmodified `main`. The rest of the module's suite covers the read paths and is unchanged. ``` mvn -o -pl langchain4j-agentic clean test Tests run: 158, Failures: 0, Errors: 0, Skipped: 0 mvn -o -pl langchain4j-agentic-mcp clean test Tests run: 26, Failures: 0, Errors: 0, Skipped: 0 mvn -o -pl langchain4j-core test Tests run: 1378, Failures: 0, Errors: 0, Skipped: 3 mvn -pl langchain4j test Tests run: 1713, Failures: 0, Errors: 0, Skipped: 19 mvn -o -pl langchain4j-agentic spotless:check BUILD SUCCESS mvn -pl langchain4j-agentic verify -DskipTests API checks completed without failures. ``` ## General checklist - [ ] 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 - [ ] I have manually run all the unit and integration tests in the module I have added/changed, and they are all green - [X] 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) The first box is unchecked because code that mutated the returned list now throws instead of succeeding, even though the Javadoc never allowed it. The fourth is unchecked because the module's integration tests need model API keys; the unit tests are green. The last three do not apply: this changes no public signature, adds no builder property and documents nothing new. Co-authored-by: Mario Fusco <mario.fusco@gmail.com>
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.