Skip to content

fix: omit system parameter when None in AnthropicLlm#5335

Open
voidborne-d wants to merge 3 commits intogoogle:mainfrom
voidborne-d:fix/anthropic-system-none
Open

fix: omit system parameter when None in AnthropicLlm#5335
voidborne-d wants to merge 3 commits intogoogle:mainfrom
voidborne-d:fix/anthropic-system-none

Conversation

@voidborne-d
Copy link
Copy Markdown

Summary

Fixes #5318.

AnthropicLlm.generate_content_async passes system=None to the Anthropic API when no system instruction is set (e.g. when LlmEventSummarizer triggers event compaction). The Anthropic API rejects None — it expects either a str, a list of content blocks, or the parameter to be omitted entirely.

Changes

  • Compute system_instruction once before the stream/non-stream branch: use the config value when present, otherwise NOT_GIVEN (which the Anthropic SDK treats as "omit this parameter").
  • Pass the resolved value through to both the non-streaming messages.create call and the streaming helper _generate_content_streaming.
  • No behavioral change when a system instruction is provided.

Reproduction

  1. Configure an ADK app with a Claude model (via Vertex AI) and EventsCompactionConfig.
  2. Run enough invocations to trigger compaction.
  3. LlmEventSummarizer creates an LlmRequest with config.system_instruction = None.
  4. Before this fix: 400 Bad Requestsystem: Input should be a valid list.
  5. After this fix: compaction succeeds; the system parameter is omitted from the API call.

@adk-bot adk-bot added the models [Component] Issues related to model support label Apr 15, 2026
@adk-bot
Copy link
Copy Markdown
Collaborator

adk-bot commented Apr 15, 2026

Response from ADK Triaging Agent

Hello @voidborne-d, thank you for submitting this pull request!

To ensure code quality and prevent future regressions, could you please add a unit test that covers the case where system_instruction is None? You can find more information about testing requirements in our contribution guidelines.

Thanks!

@voidborne-d
Copy link
Copy Markdown
Author

Thanks for the review! I've added two unit tests in 7ccb1c9:

  • test_generate_content_async_none_system_instruction_non_streaming — verifies system=NOT_GIVEN is passed (not None) when system_instruction is unset in the non-streaming path.
  • test_generate_content_async_none_system_instruction_streaming — same verification for the streaming path.

Both follow the existing patterns in test_anthropic_llm.py and assert that the Anthropic API receives NOT_GIVEN instead of None, which prevents the 400 Bad Request from #5318.

@rohityan
Copy link
Copy Markdown
Collaborator

Hi @voidborne-d , Thank you for your contribution! We appreciate you taking the time to submit this pull request. Please fix formatting errors by running autoformat.sh

@rohityan rohityan added the request clarification [Status] The maintainer need clarification or more information from the author label Apr 20, 2026
@voidborne-d voidborne-d force-pushed the fix/anthropic-system-none branch from 050fac9 to 64ddde2 Compare April 20, 2026 22:06
@voidborne-d
Copy link
Copy Markdown
Author

Hi @rohityan, thanks for the review! I've run autoformat.sh and pushed the formatting fix in 7a6b3bb. The only change was collapsing a multi-line TextBlock constructor into a single line in the test file.

@voidborne-d voidborne-d force-pushed the fix/anthropic-system-none branch from 7a6b3bb to 33ebfc7 Compare April 21, 2026 03:08
voidborne-d and others added 3 commits April 21, 2026 13:06
When system_instruction is None (e.g. during event compaction via
LlmEventSummarizer), the Anthropic API rejects system=None with a
400 Bad Request. Use NOT_GIVEN instead so the parameter is omitted
from the API call entirely.

Fixes google#5318
Adds two tests covering the fix for google#5318:
- test_generate_content_async_none_system_instruction_non_streaming
- test_generate_content_async_none_system_instruction_streaming

Both verify that system=NOT_GIVEN (not None) is passed to the Anthropic
API when system_instruction is unset, preventing 400 Bad Request errors
during event compaction.
@voidborne-d voidborne-d force-pushed the fix/anthropic-system-none branch from 33ebfc7 to c9a9896 Compare April 21, 2026 13:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

models [Component] Issues related to model support request clarification [Status] The maintainer need clarification or more information from the author

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AnthropicLlm.generate_content_async passes system=None to Anthropic API, causing 400 during event compaction

3 participants