Skip to content

fix(models): populate finish_reason on AnthropicLlm responses#5512

Open
sebastienc wants to merge 1 commit intogoogle:mainfrom
sebastienc:feat/anthropic-finish-reason
Open

fix(models): populate finish_reason on AnthropicLlm responses#5512
sebastienc wants to merge 1 commit intogoogle:mainfrom
sebastienc:feat/anthropic-finish-reason

Conversation

@sebastienc
Copy link
Copy Markdown

Summary

Fixes #5394

Disclaimer: I'm using Claude Code to generate the following text. I find the summary is better than what I would've wrote.

AnthropicLlm was calling to_google_genai_finish_reason() but never wiring its result into LlmResponse, so finish_reason was always None. This fix connects it end-to-end and expands the mapping to cover all stop reasons the current Anthropic API can return.

  • to_google_genai_finish_reason — extended to cover pause_turnSTOP (streaming interruption, e.g. server-side tool use) and refusalSAFETY (model refusal); all other unknown values continue to return FINISH_REASON_UNSPECIFIED
  • message_to_generate_content_response — passes stop_reason through to_google_genai_finish_reason and sets it on the returned LlmResponse
  • _generate_content_streaming — reads stop_reason from the message_delta event and sets finish_reason on the final aggregated LlmResponse

Testing plan

10 new tests added to tests/unittests/models/test_anthropic_llm.py. Full suite: 69 passed.

tests/unittests/models/test_anthropic_llm.py::test_to_google_genai_finish_reason[end_turn-STOP] PASSED
tests/unittests/models/test_anthropic_llm.py::test_to_google_genai_finish_reason[stop_sequence-STOP] PASSED
tests/unittests/models/test_anthropic_llm.py::test_to_google_genai_finish_reason[tool_use-STOP] PASSED
tests/unittests/models/test_anthropic_llm.py::test_to_google_genai_finish_reason[max_tokens-MAX_TOKENS] PASSED
tests/unittests/models/test_anthropic_llm.py::test_to_google_genai_finish_reason[pause_turn-STOP] PASSED
tests/unittests/models/test_anthropic_llm.py::test_to_google_genai_finish_reason[refusal-SAFETY] PASSED
tests/unittests/models/test_anthropic_llm.py::test_to_google_genai_finish_reason[None-FINISH_REASON_UNSPECIFIED] PASSED
tests/unittests/models/test_anthropic_llm.py::test_non_streaming_sets_finish_reason PASSED
tests/unittests/models/test_anthropic_llm.py::test_non_streaming_finish_reason_max_tokens PASSED
tests/unittests/models/test_anthropic_llm.py::test_streaming_sets_finish_reason PASSED

@adk-bot adk-bot added the models [Component] Issues related to model support label Apr 28, 2026
@rohityan rohityan self-assigned this Apr 30, 2026
@sebastienc sebastienc force-pushed the feat/anthropic-finish-reason branch from 4347e8a to 0a36b26 Compare May 5, 2026 13:49
@sebastienc
Copy link
Copy Markdown
Author

Fixed the pyink issue and rebased.

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 never populates finish_reason on LlmResponse

3 participants