[BUG] SupervisorPlanner NPE when the planning reply has no agentName

Author: gloriolelauCreated Sep 28, 2026Updated Sep 28, 2026

Describe the bug

SupervisorPlanner.nextSubagent dereferences the planner's AgentInvocation.getAgentName() without a null check. When the supervisor's model returns a planning reply that parses as an AgentInvocation but has no agentName, the supervisor fails with a NullPointerException instead of a descriptive error. That happens for valid JSON with the field missing, such as {"arguments": {}}, or with "agentName": null.

On current main (8daa56b97a) this is SupervisorPlanner.java:266:

java
AgentInvocation agentInvocation = withAgenticScope(...);
LOG.info("Agent Invocation: {}", agentInvocation);

if (agentInvocation.getAgentName().equalsIgnoreCase("done")) {   // NPE when agentName is absent

A few lines below, agentInvocation.getArguments().entrySet() is dereferenced the same way.

This happened to us in production with a supervisor running on deepseek-v4-flash, after several successful planning steps. When langchain4j runs under Quarkus, Json is backed by quarkus-langchain4j's QuarkusJsonCodecFactory, which ignores unknown properties. So a tool-call-shaped reply such as {"name": "someAgent", "arguments": {...}} also deserializes to an invocation with a null agentName.

Log and Stack trace

From the reproducer below (langchain4j-agentic 1.19.0-beta29):

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.equalsIgnoreCase(String)" because the return value of "dev.langchain4j.agentic.supervisor.AgentInvocation.getAgentName()" is null
	at dev.langchain4j.agentic.supervisor.SupervisorPlanner.nextSubagent(SupervisorPlanner.java:156)
	at dev.langchain4j.agentic.supervisor.SupervisorPlanner.nextAction(SupervisorPlanner.java:106)
	at dev.langchain4j.agentic.planner.Planner.firstAction(Planner.java:43)
	at dev.langchain4j.agentic.internal.PlannerBasedInvocationHandler$PlannerLoop.loop(PlannerBasedInvocationHandler.java:424)
	at dev.langchain4j.agentic.internal.PlannerBasedInvocationHandler.executeAgentMethod(PlannerBasedInvocationHandler.java:223)
	at dev.langchain4j.agentic.internal.PlannerBasedInvocationHandler.invoke(PlannerBasedInvocationHandler.java:197)
	at jdk.proxy2/jdk.proxy2.$Proxy3.invoke(Unknown Source)
	at Repro.main(Repro.java:21)

To Reproduce

Plain Java, with no framework. The classpath is langchain4j-agentic 1.19.0-beta29, langchain4j / langchain4j-core 1.19.0 and Jackson 2.22.

java
import dev.langchain4j.agentic.AgenticServices;
import dev.langchain4j.agentic.supervisor.SupervisorAgent;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.request.ChatRequest;
import dev.langchain4j.model.chat.response.ChatResponse;

public class Repro {
    public static void main(String[] args) {
        // The planner's reply is valid JSON and deserializes to AgentInvocation, but names no agent.
        ChatModel model = new ChatModel() {
            @Override
            public ChatResponse doChat(ChatRequest request) {
                return ChatResponse.builder().aiMessage(AiMessage.from("{\"arguments\": {}}")).build();
            }
        };
        SupervisorAgent supervisor = AgenticServices.supervisorBuilder()
                .chatModel(model)
                .subAgents(AgenticServices.agentAction(() -> { }))
                .build();
        supervisor.invoke("do something");
    }
}

Expected behavior

A planning reply that names no agent should be handled as an invalid plan, not as a NullPointerException. For example:

  • fail with a descriptive exception, as findAgentByName already does for an unknown name (IllegalStateException("No agent found with name: ..."));
  • or ask the planner again.

A missing arguments should likewise be treated as empty rather than dereferenced.

Please complete the following information:

  • LangChain4j version: langchain4j-agentic 1.19.0-beta29, langchain4j 1.19.0; same code on main at 8daa56b97a
  • LLM(s) used: DeepSeek deepseek-v4-flash (in production); a scripted ChatModel in the reproducer
  • Java version: 25 (Temurin 25.0.4)
  • Spring Boot version (if applicable): n/a (Quarkus 3 with quarkus-langchain4j 1.13.1 in production; the reproducer uses no framework)

Additional context

As a workaround, we wrap the supervisor's ChatModel. It checks each planning reply with the same JsonParsingUtils.extractAndParseJson(text, AgentInvocation.class) extraction the planner uses. When a reply has no agentName, the wrapper asks the model again once with a correction message, then fails with a readable error.