fix(models): skip empty text parts in Anthropic message conversion - #6983
Open
MarcHuang168 wants to merge 1 commit into
Open
fix(models): skip empty text parts in Anthropic message conversion#6983MarcHuang168 wants to merge 1 commit into
MarcHuang168 wants to merge 1 commit into
Conversation
ADK can emit Part(text='') itself (e.g. code execution with no output), and Anthropic rejects empty text blocks; the adapter fell through to NotImplementedError and failed the whole request. Skip such parts, matching the LiteLLM path. Prepared with Claude assistance; reviewed and tested by author.
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.
Link to Issue or Description of Change
Link to an existing issue:
Closes: #6982
Problem:
AnthropicLlmconverts eachPartinto an Anthropic content block in_part_to_message_block. A part carrying only an empty text string (Part(text='')) matches no branch —if part.text:is false for''— and falls through to the finalraise NotImplementedError("Not supported yet: ..."), failing the whole model call with a misleading error.ADK emits such parts itself:
code_executors/code_execution_utils.pywritesPart(text='')into the content when a code execution produces no output. The LiteLLM path already tolerates empty text parts, so the same conversation works viaLiteLlmbut fails via the nativeAnthropicLlmadapter.Solution:
Add
_is_empty_text_part()(a part whose only payload is an emptytext, optionally withthought) and skip such parts in_content_to_message_param, logging at debug level. This mirrors the adapter's existing warn-and-skip handling of unsupported media in assistant turns, and Anthropic rejects empty text blocks anyway. Parts that carry other payload alongside empty text (e.g. afunction_call) are unaffected, and theNotImplementedErrorfallback for genuinely unsupported part types is kept.Testing Plan
Unit Tests:
Added in
tests/unittests/models/test_anthropic_llm.py:test_content_to_message_param_skips_empty_text_part— an empty text part is dropped and the remaining text block is preserved.test_content_to_message_param_keeps_non_text_payload_with_empty_text— a part withtext=''plus afunction_callstill produces atool_useblock.Verified the first test fails without the fix:
With the fix:
pre-commit run --files <the two changed files>: all hooks pass.Manual End-to-End (E2E) Tests:
Ran the reproduction from #6982 (
content_to_message_paramon aContentwithPart(text="run it")andPart(text="")). Before:NotImplementedError: Not supported yet: ... text='' .... After:{'role': 'user', 'content': [{'text': 'run it', 'type': 'text'}]}.Checklist
Additional context
Prepared with Claude assistance; the bug was reproduced, and the change reviewed and tested, by me.