Skip to content

fix(anthropic): report the stop reason of a response as its finish reason - #3852

Merged
jujn merged 1 commit into
agentscope-ai:mainfrom
pcbeingused33:fix/anthropic-finish-reason
Oct 30, 2026
Merged

jujn merged 1 commit into
agentscope-ai:mainfrom
pcbeingused33:fix/anthropic-finish-reason

Conversation

@pcbeingused33

Copy link
Copy Markdown
Contributor

AgentScope-Java Version

2.1.0-SNAPSHOT (main)

Description

ChatResponse has a finishReason, which ReasoningContext uses to detect a reply cut off at the token limit (length, max_tokens; its comment already expects Anthropic to report max_tokens). The OpenAI, DashScope, Gemini and Ollama parsers fill it in, but AnthropicResponseParser never did, so every Anthropic response had a null finish reason and a reply truncated at max_tokens looked like a normal completion.

Fix: parseMessage now sets finishReason from Message.stopReason(), and the streaming path reads it from the message_delta event (delta().stopReason()), where Anthropic reports it. The value is the API's own string (end_turn, max_tokens, stop_sequence, tool_use, ...), the same style the other parsers use, and it stays null when the API sends no stop reason.

Tests: AnthropicResponseParserTest gains cases for a non-streamed message with max_tokens, one without a stop reason, and a streamed message_delta with max_tokens. The two existing tests that mock a message_delta event now also mock its delta(), which the parser reads. The new cases fail on main (expected: <max_tokens> but was: <null>) and pass with the change:

mvn -pl agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-anthropic -am test -Dtest=AnthropicResponseParserTest

No request was sent to the live API.

This change was prepared with the assistance of an AI coding agent (Claude).

Checklist

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn test) — the Anthropic module's tests; the full suite was not run
  • Javadoc comments are complete and follow project conventions — no public API added
  • Related documentation has been updated (e.g. links, examples, etc.) — not needed
  • Code is ready for review

This change was prepared with the assistance of an AI coding agent (Claude).

…ason

AnthropicResponseParser never set ChatResponse.finishReason, unlike the OpenAI,
DashScope, Gemini and Ollama parsers, so a reply cut off at max_tokens looked
like a normal completion. Read it from Message.stopReason() for a complete
message and from the message_delta event when streaming.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@jujn
jujn merged commit b604eca into agentscope-ai:main Oct 30, 2026
3 of 4 checks passed
Buktal added a commit to Buktal/agentscope-java that referenced this pull request Oct 30, 2026
Resolve conflicts with agentscope-ai#3852 in AnthropicResponseParser(.Test):
keep this branch's native thinking signature/redacted-thinking replay
and content-block metadata, and adopt main's finishReason handling
(StopReason::asString wire value) in parseMessage and message_delta.
chickenlj pushed a commit that referenced this pull request Nov 13, 2026
…ason (#3852)

Backport the verified fix to the 2.0.x implementation and retain its
existing public contracts where the upstream kernel differs.

(cherry picked from commit b604eca)
Constraint: No AgentScope Service or SDK migration
Confidence: high
Not-tested: Whole-repository verification pending after lane integration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants