Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions provider/anthropicprovider/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,9 @@ func (a *client) run(ctx context.Context, messages []*message.Message, options .
}

// mapStopReason maps an Anthropic stop_reason to the canonical FinishReason
// values shared across providers (mirroring the OpenAI/Copilot providers). It
// returns "" for empty or unrecognized reasons so callers can fall through.
// values shared across providers (mirroring the OpenAI/Copilot providers). An
// empty reason returns ""; an unrecognized non-empty reason is passed through
// unchanged so newer Anthropic values still reach the caller.
func mapStopReason(reason anthropic.StopReason) string {
switch reason {
case anthropic.StopReasonEndTurn, anthropic.StopReasonStopSequence, anthropic.StopReasonPauseTurn:
Expand All @@ -253,7 +254,11 @@ func mapStopReason(reason anthropic.StopReason) string {
case anthropic.StopReasonRefusal:
return "content_filter"
default:
return ""
// 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)
Comment on lines +257 to +261
}
}

Expand Down
3 changes: 3 additions & 0 deletions provider/anthropicprovider/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,9 @@ var finishReasonCases = []struct {
{"max_tokens", "length"},
{"tool_use", "tool_calls"},
{"refusal", "content_filter"},
// Unmapped stop reasons pass through unchanged so newer Anthropic values
// still reach the caller.
{"model_context_window_exceeded", "model_context_window_exceeded"},
}

// TestNonStreamingFinishReason verifies the provider maps the Anthropic
Expand Down
Loading