{"record":{"id":"1def8748ba2486b3","repo":"alibaba/spring-ai-alibaba","slug":"chatmodel-must-be-provided-for-llm-routing-agent","errorCode":null,"errorMessage":"ChatModel must be provided for LLM routing agent","messagePattern":"ChatModel must be provided for LLM routing agent","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"spring-ai-alibaba-agent-framework/src/main/java/com/alibaba/cloud/ai/graph/agent/flow/agent/LlmRoutingAgent.java","lineNumber":108,"sourceCode":"\t\t\tthis.systemPrompt = systemPrompt;\n\t\t\treturn this;\n\t\t}\n\n\t\tpublic LlmRoutingAgentBuilder instruction(String instruction) {\n\t\t\tthis.instruction = instruction;\n\t\t\treturn this;\n\t\t}\n\n\t\t@Override\n\t\tprotected LlmRoutingAgentBuilder self() {\n\t\t\treturn this;\n\t\t}\n\n\t\t@Override\n\t\tprotected void validate() {\n\t\t\tsuper.validate();\n\t\t\tif (chatModel == null) {\n\t\t\t\tthrow new IllegalArgumentException(\"ChatModel must be provided for LLM routing agent\");\n\t\t\t}\n\t\t}\n\n\t\t@Override\n\t\tpublic LlmRoutingAgent doBuild() {\n\t\t\tvalidate();\n\t\t\treturn new LlmRoutingAgent(this);\n\t\t}\n\n\t}\n\n}\n","sourceCodeStart":90,"sourceCodeEnd":121,"githubUrl":"https://github.com/alibaba/spring-ai-alibaba/blob/f82da0b50f35744c13968191be2b1cd2452ef550/spring-ai-alibaba-agent-framework/src/main/java/com/alibaba/cloud/ai/graph/agent/flow/agent/LlmRoutingAgent.java#L90-L121","documentation":"LlmRoutingAgent delegates routing decisions to an LLM, so its builder's validate() (invoked from doBuild()) requires a ChatModel. Building without one means the agent could not choose among its sub-agents, so it fails fast with IllegalArgumentException.","triggerScenarios":"Calling LlmRoutingAgent.builder()...build() (which calls doBuild -> validate) without a chatModel(...) call, or passing null to chatModel().","commonSituations":"Assembling the agent in a conditional code path where the model bean is null due to a missing DashScope/OpenAI starter or API key; refactoring that removed the model builder call; copying a graph-structured agent example that does not need a model into an LLM-routing agent.","solutions":["Add chatModel(model) to the builder with a configured ChatModel bean (e.g., DashScopeChatModel or OpenAiChatModel).","Verify the model bean actually initialized — a null usually indicates a missing starter dependency or API key so the auto-configured bean was absent.","If routing should be rule-based instead, use a different (non-LLM) routing agent type that does not require a ChatModel."],"exampleFix":"// before\nLlmRoutingAgent agent = LlmRoutingAgent.builder()\n    .name(\"router\")\n    .subAgents(specA, specB)\n    .build(); // throws: ChatModel must be provided\n// after\nLlmRoutingAgent agent = LlmRoutingAgent.builder()\n    .name(\"router\")\n    .chatModel(chatModel)\n    .subAgents(specA, specB)\n    .build();","handlingStrategy":"validation","validationCode":"Objects.requireNonNull(chatModel, \"LlmRoutingAgent requires a configured ChatModel bean\");\nLlmRoutingAgent agent = LlmRoutingAgent.builder().chatModel(chatModel)...build();","typeGuard":"static boolean readyForBuild(LlmRoutingAgent.Builder b) { return b != null; } // ensure model injected via constructor/bean before building","tryCatchPattern":"try {\n    agent = llmRoutingBuilder.build();\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"ChatModel\")) {\n        log.error(\"ChatModel bean missing; check DashScope/OpenAI starter and API key\");\n    }\n}","preventionTips":["Confirm the chat model auto-configuration loaded (starter dependency + API key) before building agents.","Inject the ChatModel via Spring and assert its presence in a @PostConstruct check.","Keep model wiring in one configuration class so it cannot be dropped during refactors."],"tags":["java","llm","routing-agent","builder-validation"],"backgroundTag":"missing-required-argument","analyzedSha":"f82da0b50f35744c13968191be2b1cd2452ef550","analyzedAt":"2026-09-09T15:32:42.421Z","contentChangedAt":"2026-09-09T15:32:42.421Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}