Skip to content

LangChain4j.toParts() throws NPE when AiMessage.text() is null #1034

@anandnair2005

Description

@anandnair2005

Description

LangChain4j.toParts() throws a NullPointerException when processing an AiMessage with a null text field. This occurs in multi-agent setups where the LLM returns tool-call-only responses (no text content).

The toParts() method passes aiMessage.text() directly to Part.builder().text() without a null check. The AutoValue-generated Part.Builder.text() rejects null values, causing the NPE.

AiMessage.text() returning null is a known LangChain4j behavior for tool-call-only responses — see langchain4j#2686 and langchain4j#986.

Stack Trace

java.lang.NullPointerException
    at com.google.adk.models.langchain4j.AutoValue_Part$Builder.text(AutoValue_Part.java)
    at com.google.adk.models.langchain4j.LangChain4j.toParts(LangChain4j.java)
    at com.google.adk.models.langchain4j.LangChain4j.lambda$generateContent$1(LangChain4j.java)

Steps to Reproduce

  1. Set up a router agent with sub-agents using the LangChain4j bridge
  2. Use an OpenAI-compatible provider (e.g., OpenRouter) as the LLM backend
  3. Send a query that triggers sub-agent delegation
  4. When the LLM response contains tool calls but no text, toParts() crashes

The issue is intermittent (~1 in 10-20 requests) because it depends on whether the LLM includes a text field alongside tool calls.

Expected Behavior

Null text should be handled gracefully — skip the text part when aiMessage.text() is null, rather than crashing.

Affected Versions

0.7.0, 0.8.0, 0.9.0

Environment

  • Java 21+
  • LangChain4j with OpenRouter (OpenAI-compatible API)
  • Multi-agent router with specialist sub-agents
  • Cloud Run deployment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions