fix: avoid passing system=None to Anthropic API when no system instruction is set#5334
fix: avoid passing system=None to Anthropic API when no system instruction is set#5334geomarceau wants to merge 2 commits intogoogle:mainfrom
Conversation
…ction is set When llm_request.config.system_instruction is None, passing it directly to messages.create causes a 400 error from the Anthropic API. Use NOT_GIVEN instead so the parameter is omitted from the request entirely. Also thread the system value through to _generate_content_streaming so both code paths are consistent. Fixes google#5318
|
Response from ADK Triaging Agent Hello @geomarceau, thank you for creating this PR! To help reviewers verify the fix and understand its impact, could you please add a This information will help us review your PR more efficiently. Thanks! |
|
Hi @geomarceau , Thank you for your contribution! We appreciate you taking the time to submit this pull request. Can you add a unit test case that that covers the case where generate_content_async is called without a system instruction and also fix formatting errors by running autoformat.sh. |
|
Hi @geomarceau closing this since this is a duplicate of #5335 which already has comprehensive unit tests added. |
When no system instruction is configured, llm_request.config.system_instruction is None. Passing that directly as the system argument to messages.create triggers a 400 error from the Anthropic API because the API requires the parameter to be absent rather than explicitly null.
The fix computes a local system variable that is either the actual instruction string or NOT_GIVEN, so the parameter is omitted from the request when there is nothing to send. The same value is threaded through to _generate_content_streaming so both the streaming and non-streaming code paths behave consistently.
Fixes #5318
Testing
To reproduce the original bug, create an AnthropicLlm instance and call it without a system instruction. Before this fix the system kwarg was None going into messages.create, which the Anthropic API rejects with a 400.
With the fix in place, the following script confirms the behavior:
Output with fix applied (system kwarg captured from the mock client):
The existing test suite also covers the case where a system instruction is present and verifies it is forwarded correctly.