fix: avoid passing system=None to Anthropic API when no system instruction is set#5334
Open
geomarceau wants to merge 2 commits intogoogle:mainfrom
Open
fix: avoid passing system=None to Anthropic API when no system instruction is set#5334geomarceau wants to merge 2 commits intogoogle:mainfrom
geomarceau 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
Collaborator
|
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! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.