[bug][integrations][anthropic][java][python] Preserve Anthropic finish reason - #1107
Open
emecii wants to merge 1 commit into
Open
[bug][integrations][anthropic][java][python] Preserve Anthropic finish reason#1107emecii wants to merge 1 commit into
emecii wants to merge 1 commit into
Conversation
Map Anthropic max_tokens responses to the shared length finish reason in both SDK integrations, while preserving other stop reasons. Generated-by: Codex CLI 0.153.4 (GPT-6)
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.
Linked issue: #936
Purpose of change
Anthropic responses that stop at the configured output-token limit now reach the shared chat action
as
extra_args["finish_reason"] == "length". The action already rejects that canonical value beforeit dispatches incomplete content, so callers receive its clear truncation error instead of parsing
or consuming a partial response.
Runtime flow
Both Anthropic connections read the SDK response stop reason after converting the content. They map
Anthropic's
max_tokensvalue to the existing cross-providerlengthvalue and retain every otherreported value unchanged. The shared chat action reads that metadata before it dispatches the
response, while direct connection callers can inspect the metadata themselves.
Behavioral Semantics
Key decisions
Only
max_tokensis normalized because it is Anthropic's name for the shared action's establishedtoken-limit condition. Other reasons remain provider values so that an unknown future reason is
observable without changing control flow.
Interaction decisions
finish_reasonmax_tokenslengthBehavioral contracts
max_tokensexposes the canonicallengthmetadata that the shared action recognizes.
Failure behavior
This converter change does not catch, retry, or transform provider failures. When the model returns
max_tokens, the existing shared action raises its established truncation error; direct connectioncallers can inspect the response metadata before invoking that action.
Tests
max_tokensbecomeslength; ordinary reasons remain observableAnthropicChatModelConnectionTest(94 passed); Pythontest_anthropic_response_parsing.pylengthbefore dispatchChatModelActionTestandChatModelActionRetryTest(36 passed); Pythontest_chat_model_action.pyFocused Python run: 107 passed across the Anthropic response parser and shared-action tests.
Spotless, Ruff format/check, and Apache RAT license checks pass.
Coverage and gaps
Focused Java and Python tests cover the provider-to-shared-reason mapping and the shared action's
rejection path. Live Anthropic API calls were not run; the tests use SDK response objects and local
action fixtures, so they require no credentials or network access.
Implementation evidence
The Python converter stores the key independently of token-usage metrics. The Java converter reads
the optional SDK stop reason after constructing the response and does the same. This keeps the
mapping available for responses with or without recorded usage and for both plain-text and tool-call
conversion paths.
API
Compatibility impact
No public API signature changes. Existing consumers of
extra_argsgainfinish_reasononAnthropic responses; the shared action now classifies Anthropic token-limit responses consistently
with OpenAI-family responses.
Documentation
doc-neededdoc-not-neededdoc-includedWas this patch authored or co-authored using generative AI tooling?
Generated-by: Codex CLI 0.153.4 (GPT-6)