Skip to content

Pass through unmapped Anthropic stop reasons - #1117

Open
PratikDhanave (PratikDhanave) wants to merge 2 commits into
microsoft:mainfrom
PratikDhanaveFork:fix/anthropic-stop-reason-passthrough
Open

PratikDhanave (PratikDhanave) wants to merge 2 commits into
microsoft:mainfrom
PratikDhanaveFork:fix/anthropic-stop-reason-passthrough

Conversation

@PratikDhanave

Copy link
Copy Markdown
Contributor

mapStopReason returned "" for any stop_reason not in its fixed set, so a value the Anthropic API may add later — e.g. model_context_window_exceeded — was reported as no finish reason instead of reaching the caller. This affects both the non-streaming path and the streaming final update.

The Python client passes unmapped reasons through unchanged: FINISH_REASON_MAP.get(stop_reason, stop_reason), with an explicit docstring that newer API values like model_context_window_exceeded must still reach the caller.

Change

  • Change the default case to return string(reason). An empty reason still yields "", so the streaming if fr != "" guard is unaffected. StopReason is a plain string in the anthropic SDK.

Test

  • Adds a {"model_context_window_exceeded", "model_context_window_exceeded"} row to the shared finishReasonCases table (runs through both TestNonStreamingFinishReason and TestStreamingFinishReason). Fails before (""), passes after.

mapStopReason returned "" for any stop_reason outside its fixed set, so a
newer value such as model_context_window_exceeded was reported as no finish
reason at all. Pass unmapped reasons through unchanged, matching the Python
client (an empty reason still maps to "").
Copilot AI lite review requested due to automatic review settings September 20, 2026 04:22
@github-actions github-actions Bot added area:provider Changes files in the provider area area:provider/anthropic Changes files in the provider / anthropic area size:small At most 30 changed lines across at most 2 files labels Sep 20, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Update the stale function comment to document the new pass-through behavior.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 Low severity

Open (1)
What changed in this PR

Updates Anthropic stop-reason handling so unknown non-empty values reach callers unchanged.

Changes:

  • Passes through unmapped stop reasons.
  • Adds non-streaming and streaming test coverage.
File Description
provider/​anthropicprovider/​agent.go Passes through unknown stop reasons.
provider/​anthropicprovider/​agent_test.go Tests pass-through behavior in both paths.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +256 to +260
// Pass through stop reasons we don't explicitly map (e.g. a newer
// value like model_context_window_exceeded) so they still reach the
// caller instead of being reported as no finish reason, matching the
// Python client. An empty reason maps to "".
return string(reason)
@github-actions github-actions Bot added kind:code Changes production behavior or code kind:tests Changes tests, fixtures, or test infrastructure labels Sep 20, 2026
@github-actions

This comment has been minimized.

Update the stale comment to reflect that only an empty reason returns "";
unrecognized non-empty reasons are now passed through unchanged.
@github-actions github-actions Bot added size:medium At most 100 changed lines across at most 5 files and removed size:small At most 30 changed lines across at most 2 files labels Sep 21, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Scope: user-visible behavior

Changed Go contract: No exported symbols changed, but observable runtime behavior does: the FinishReason value returned to callers of the Anthropic provider's Run/streaming APIs for a previously-unmapped stop_reason changes from "" (no finish reason) to the raw reason string (e.g. model_context_window_exceeded). mapStopReason itself remains an unexported helper in provider/anthropicprovider/agent.go.

Upstream evidence reviewed: python/packages/anthropic/agent_framework_anthropic/_chat_client.pyFINISH_REASON_MAP and _map_finish_reason() (lines ~239–258). The Python docstring explicitly states: "Stop reasons that are not in FINISH_REASON_MAP are passed through unchanged so that values added by the Anthropic API, such as model_context_window_exceeded, still reach the caller instead of being reported as no finish reason at all," implemented via FINISH_REASON_MAP.get(stop_reason, stop_reason). No .NET Anthropic-specific stop-reason mapping table was found under dotnet/src/Microsoft.Agents.AI.Anthropic to compare against.

Result: aligned. This change makes the Go mapStopReason default case return string(reason) instead of "", exactly mirroring the Python pass-through behavior (and the same example value, model_context_window_exceeded) cited in the PR description and confirmed above. It's a bug fix that removes a previous Go-only divergence (silently dropping unmapped stop reasons as "") rather than introducing one — Go now matches the documented Python contract. No exported Go API/type/signature changed, so public-api-change is not applicable.

Generated by Go API Consistency Review Agent · copilot · auto · 27.6 AIC · ⌖ 5.57 AIC · ⊞ 9.2K ·

@github-actions github-actions Bot added parity-approved Go API consistency review found no parity issues kind:dependencies Changes dependencies or manifests labels Sep 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:provider/anthropic Changes files in the provider / anthropic area area:provider Changes files in the provider area kind:code Changes production behavior or code kind:dependencies Changes dependencies or manifests kind:tests Changes tests, fixtures, or test infrastructure parity-approved Go API consistency review found no parity issues size:medium At most 100 changed lines across at most 5 files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants