Skip to content

fix: avoid passing system=None to Anthropic API when no system instruction is set#5334

Open
geomarceau wants to merge 2 commits intogoogle:mainfrom
geomarceau:fix/anthropic-system-not-given
Open

fix: avoid passing system=None to Anthropic API when no system instruction is set#5334
geomarceau wants to merge 2 commits intogoogle:mainfrom
geomarceau:fix/anthropic-system-not-given

Conversation

@geomarceau
Copy link
Copy Markdown

@geomarceau geomarceau commented Apr 14, 2026

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:

llm = AnthropicLlm(model="claude-3-5-sonnet-20241022")
request = LlmRequest(model="claude-3-5-sonnet-20241022")
request.contents = [Content(role="user", parts=[Part(text="Hello")])]
async for _ in llm.generate_content_async(request):
    pass

Output with fix applied (system kwarg captured from the mock client):

system value passed to messages.create: NOT_GIVEN
Is NOT_GIVEN: True
Is None: False
PASS: system parameter omitted correctly (NOT_GIVEN), no 400 error will occur

The existing test suite also covers the case where a system instruction is present and verifies it is forwarded correctly.

…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
@adk-bot adk-bot added the models [Component] Issues related to model support label Apr 14, 2026
@adk-bot
Copy link
Copy Markdown
Collaborator

adk-bot commented Apr 14, 2026

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 testing plan section to your PR description and provide logs or a screenshot demonstrating that the fix works as expected?

This information will help us review your PR more efficiently. Thanks!

@rohityan rohityan self-assigned this Apr 16, 2026
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

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