Subhash Polisetti b8b809ed57 fix: Map an AiMessage without text and without tool calls instead of failing (#6123)
## Issue

Closes #6122

## Change

`toOpenAiMessage` has two branches for an `AiMessage`, and only the
second one tolerates null text:

```java
if (!aiMessage.hasToolExecutionRequests()) {
    .content(aiMessage.text())                                 // threw on null
}
...
    .content(aiMessage.text() != null ? aiMessage.text() : "") // already guarded
```

`ChatCompletionAssistantMessageParam.Builder.content(String)` is
non-null in the SDK, so a message with no
text and no tool calls threw `NullPointerException` before the request
was sent. The fix applies the guard the
other branch already uses, so both behave alike.

The message is one this module produces:
`OpenAiOfficialStreamingChatModel` builds its result with
`.text(text.isEmpty() ? null : text)`, so a stream returning neither
text nor tool calls yields an
`AiMessage` with null text and no tool execution requests. Once that is
in the chat memory, the next call in
the conversation fails while mapping it.

The empty string is what the other branch already substitutes for the
same value, so both branches now send
the same thing for the same input. The SDK does allow the field to be
omitted instead, and some other
integrations skip the text when it is null, but they build a list of
content parts where omitting one is
natural; here `content` is a single field, and omitting it in one branch
while the other sends `""` would
just move the inconsistency rather than remove it.

### Tests

- `InternalOpenAiOfficialHelperTest` (existing class, 2 new tests): an
`AiMessage` with no text and no tool
calls now maps to empty content instead of throwing; an `AiMessage` that
does carry text still maps to that
  text

The first fails on unmodified `main` with the `NullPointerException`
above. The second passes either way, it
pins the behaviour that must not change.

```
mvn -pl langchain4j-open-ai-official test
Tests run: 20, Failures: 0, Errors: 0, Skipped: 0

mvn -pl langchain4j-open-ai-official verify -DskipITs
revapi: API checks completed without failures

langchain4j-core: Tests run: 1266, Failures: 0, Errors: 0, Skipped: 5
langchain4j:      Tests run: 1358, Failures: 0, Errors: 0, Skipped: 0
```

OpenAI integration tests need an API key and were not run.

## 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
- [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)
2026-08-21 13:49:57 +02:00
2026-08-20 09:58:19 +02:00
2023-12-19 11:37:11 +01:00
2023-06-20 17:30:29 +02:00

LangChain4j: idiomatic, open-source Java library for building LLM-powered applications on the JVM

Build Status Nightly Build CODACY

Discord BlueSky X Maven Version

Introduction

Welcome!

The goal of LangChain4j is to simplify integrating LLMs into Java applications.

Here's how:

  1. 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.
  2. 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.
  3. 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:

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.

S
Description
LangChain4j 是面向 JVM 的 Java 开源库,为构建 LLM 应用提供统一 API,支持工具调用、MCP、Agent 和 RAG。|GitHub 镜像 12.9k · 🍴 2.5k
https://github.com/langchain4j/langchain4j Readme Apache-2.0 312 MiB
Languages
Java 99.5%
Kotlin 0.4%
Shell 0.1%