Skip to content

[bug][integrations][anthropic][java][python] Preserve Anthropic finish reason - #1107

Open
emecii wants to merge 1 commit into
apache:mainfrom
emecii:fix/anthropic-finish-reason
Open

[bug][integrations][anthropic][java][python] Preserve Anthropic finish reason#1107
emecii wants to merge 1 commit into
apache:mainfrom
emecii:fix/anthropic-finish-reason

Conversation

@emecii

@emecii emecii commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

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 before
it 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_tokens value to the existing cross-provider length value and retain every other
reported 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_tokens is normalized because it is Anthropic's name for the shared action's established
token-limit condition. Other reasons remain provider values so that an unknown future reason is
observable without changing control flow.

Interaction decisions

Anthropic stop reason Stored finish_reason Shared action behavior
absent absent Existing behavior
max_tokens length Rejects incomplete response before dispatch
any other supplied value same value Existing accepted-reason behavior

Behavioral contracts

  • A Java or Python Anthropic response stopped by max_tokens exposes the canonical length
    metadata that the shared action recognizes.
  • A supplied non-token-limit stop reason remains available without being reclassified.
  • A response without a stop reason gains no metadata entry.

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 connection
callers can inspect the response metadata before invoking that action.

Tests

Contract Tests
max_tokens becomes length; ordinary reasons remain observable Java AnthropicChatModelConnectionTest (94 passed); Python test_anthropic_response_parsing.py
Shared action rejects length before dispatch Java ChatModelActionTest and ChatModelActionRetryTest (36 passed); Python test_chat_model_action.py

Focused 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_args gain finish_reason on
Anthropic responses; the shared action now classifies Anthropic token-limit responses consistently
with OpenAI-family responses.

Documentation

  • doc-needed
  • doc-not-needed
  • doc-included

Was this patch authored or co-authored using generative AI tooling?

  • Yes
  • No

Generated-by: Codex CLI 0.153.4 (GPT-6)

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)
@github-actions github-actions Bot added doc-not-needed Your PR changes do not impact docs fixVersion/0.4.0 priority/major Default priority of the PR or issue. labels Sep 7, 2026
@github-actions github-actions Bot added doc-not-needed Your PR changes do not impact docs and removed doc-not-needed Your PR changes do not impact docs labels Sep 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

doc-not-needed Your PR changes do not impact docs fixVersion/0.4.0 priority/major Default priority of the PR or issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant