Skip to content

Commit f1d3cc5

Browse files
authored
fix: prevent panic when LLMAgent has no Model configured (google#280)
Add nil check in Flow.callLLM() after BeforeModelCallbacks but before accessing Model.GenerateContent(). This prevents a nil pointer dereference panic when an llmagent is created without a Model configuration. The check is positioned after BeforeModelCallbacks to allow callbacks that return cached responses to short-circuit execution without requiring a Model. This provides a clear error message instead of a cryptic segmentation fault: 'agent %q has no Model configured; ensure Model is set in llmagent.Config' Preserves valid use cases: - Testing agent metadata/structure without running the agent - BeforeAgentCallbacks that short-circuit before agent execution - BeforeModelCallbacks that return cached responses Fixes panic: runtime error: invalid memory address or nil pointer dereference when sub-agents without Models were invoked.
1 parent 7f5fe5e commit f1d3cc5

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

‎internal/llminternal/base_flow.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,11 @@ func (f *Flow) callLLM(ctx agent.InvocationContext, req *model.LLMRequest, state
248248
}
249249
}
250250

251+
if f.Model == nil {
252+
yield(nil, fmt.Errorf("agent %q has no Model configured; ensure Model is set in llmagent.Config", ctx.Agent().Name()))
253+
return
254+
}
255+
251256
// TODO: Set _ADK_AGENT_NAME_LABEL_KEY in req.GenerateConfig.Labels
252257
// to help with slicing the billing reports on a per-agent basis.
253258

0 commit comments

Comments
 (0)