Skip to content

Pass through unmapped Anthropic stop reasons - #1117

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

PratikDhanave (PratikDhanave) wants to merge 4 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

This comment has been minimized.

@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
@qmuntal

Copy link
Copy Markdown
Member

Copilot resolve the merge conflicts in this pull request

…reason-passthrough

# Conflicts:
#	go.mod
#	go.sum
@github-actions github-actions Bot added size:small At most 30 changed lines across at most 2 files and removed size:medium At most 100 changed lines across at most 5 files kind:dependencies Changes dependencies or manifests labels Oct 3, 2026
@github-actions

This comment has been minimized.

@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

API consistency review

Result Scope
aligned user-visible behavior

Changed Go contract

mapStopReason in provider/anthropicprovider/agent.go now passes unmapped non-empty Anthropic stop_reason values through as the finish reason, instead of returning "". No exported API changed.

Upstream evidence reviewed

Implementation Source Contract checked
Python python/packages/anthropic/agent_framework_anthropic/_chat_client.py (FINISH_REASON_MAP) Unmapped stop reasons pass through unchanged.
Python test python/packages/anthropic/tests/test_anthropic_client.py model_context_window_exceeded passes through.

Assessment

The Go change matches Python's passthrough behavior, and the added test case covers the non-streaming and streaming paths. I did not review the .NET adapter. No parity issues found.

Generated by Go API Consistency Review Agent · copilot · auto · 16.3 AIC · ⌖ 0.619 AIC · ⊞ 5.1K · ◷

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:tests Changes tests, fixtures, or test infrastructure parity-approved Go API consistency review found no parity issues size:small At most 30 changed lines across at most 2 files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants