Skip to content

Normalize finish_reason across providers#321

Open
cpsievert wants to merge 1 commit into
mainfrom
feat/finish-reason-normalization
Open

Normalize finish_reason across providers#321
cpsievert wants to merge 1 commit into
mainfrom
feat/finish-reason-normalization

Conversation

@cpsievert

Copy link
Copy Markdown
Collaborator

Mirrors tidyverse/ellmer#1015.

What's now possible

turn.finish_reason returns a consistent vocabulary regardless of which provider you're using:

Value Meaning
"success" Normal completion
"max_tokens" Hit token limit
"content_filter" Blocked by safety filter
"stop_sequence" Hit a stop sequence
"context_window" Context window exceeded

Unknown provider-specific reasons pass through as-is so callers can still inspect them.

Changes

  • Each provider module gains a normalize_finish_reason() function with a mapping dict keyed to the upstream API docs
  • AssistantTurn.finish_reason is now set by calling that function rather than storing the raw provider string
  • OpenAI streaming fix: response.incomplete events are now handled in stream_merge_chunks so truncated streaming responses are returned rather than silently dropped
  • All finish_reason == "stop" / "end_turn" / "STOP" assertions in provider tests updated to "success"
  • Unit tests added for each provider's normalization function covering known mappings, unknown pass-through, and None handling

Maps provider-specific stop reasons to a common vocabulary:
"success", "max_tokens", "content_filter", "stop_sequence",
"context_window". Unknown reasons pass through as-is.

Also fixes OpenAI streaming to handle `response.incomplete` events
so truncated responses are returned rather than silently dropped.

Mirrors tidyverse/ellmer#1015.
@thisisnic

Copy link
Copy Markdown

FYI, I've extended the list of standard values in tidyverse/ellmer#1039 to also include "tool_use" as it was raising unnecessary warnings.

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