fix: handle null AiMessage.text() in LangChain4j.toParts()#1035
fix: handle null AiMessage.text() in LangChain4j.toParts()#1035anandnair2005 wants to merge 1 commit intogoogle:mainfrom
Conversation
AiMessage.text() can be null when the LLM returns a tool-call-only response (e.g., during sub-agent delegation). The AutoValue-generated Part.Builder.text() rejects null, causing an NPE. Add a null guard to skip text parts when text is null.
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a critical NullPointerException in the LangChain4j utility when processing AiMessage instances where the text content is null, a common occurrence in multi-agent or tool-call-only LLM interactions. The change introduces a robust null check, preventing application crashes and improving the stability of LLM response handling. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request effectively addresses a NullPointerException in LangChain4j.toParts() when handling an AiMessage with null text, a valid case for tool-call-only responses. The fix is straightforward, adding a null check to return an empty list. The inclusion of a dedicated unit test is a great addition to verify the fix and prevent regressions. I have one minor suggestion to improve the code's clarity.
| if (aiMessage.text() == null) { | ||
| return List.of(); | ||
| } | ||
| Part part = Part.builder().text(aiMessage.text()).build(); | ||
| return List.of(part); |
There was a problem hiding this comment.
While the logic is correct, this block can be made slightly more efficient and readable by avoiding the repeated call to aiMessage.text(). Storing the result in a local variable first makes the code cleaner and more concise.
| if (aiMessage.text() == null) { | |
| return List.of(); | |
| } | |
| Part part = Part.builder().text(aiMessage.text()).build(); | |
| return List.of(part); | |
| String text = aiMessage.text(); | |
| if (text == null) { | |
| return List.of(); | |
| } | |
| return List.of(Part.builder().text(text).build()); |
Summary
LangChain4j.toParts()throwsNullPointerExceptionwhenAiMessage.text()is null (tool-call-only LLM responses in multi-agent setups)Fixes #1034
Details
AiMessage.text()can legitimately benullwhen the LLM returns a tool-call-only response (e.g., during sub-agent delegation). The AutoValue-generatedPart.Builder.text()rejects null, causing an NPE. This is a known LangChain4j behavior — see langchain4j#2686 and langchain4j#986.The fix adds a null check in the
elsebranch oftoParts()— whenaiMessage.text()is null, an empty list is returned instead of crashing.Test plan
testGenerateContentWithNullAiMessageText— mocksAiMessagewith null text and no tool requests, asserts no NPE and empty parts listtestStreamingRunConfig(OpenAI) passestestAgentToolandtestSubAgenterror due to pre-existing issue: hardcodedgemini-2.0-flashmodel has been retired by Google (unrelated to this change)./mvnw clean package -pl contrib/langchain4j -am