Skip to content

fix(@effect/ai-openai): map max_output_tokens finish reason to length#6297

Merged
IMax153 merged 2 commits into
Effect-TS:mainfrom
shtse8:feat/openai-max-output-tokens-finish-reason
Jun 29, 2026
Merged

fix(@effect/ai-openai): map max_output_tokens finish reason to length#6297
IMax153 merged 2 commits into
Effect-TS:mainfrom
shtse8:feat/openai-max-output-tokens-finish-reason

Conversation

@shtse8

@shtse8 shtse8 commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

What

@effect/ai-openai's internal finishReasonMap (packages/ai/openai/src/internal/utilities.ts) was missing the OpenAI Responses API truncation reason max_output_tokens.

The Responses API reports a budget/output-length truncation as a response.incomplete lifecycle terminal with incomplete_details.reason: "max_output_tokens" — distinct from the Chat Completions finish_reason: "length". resolveFinishReason reads response.incomplete_details?.reason and looks it up in finishReasonMap; with max_output_tokens absent, it fell through to:

return hasToolCalls ? "tool-calls" : "unknown"

So a genuinely length-truncated Responses turn surfaced finishReason: "unknown" instead of the portable "length". Consumers that treat "unknown" as a transport-level / no-terminal-finish failure (rather than a real, recoverable budget truncation) then mis-handle a perfectly valid truncated response.

Change

Add max_output_tokens: "length" to finishReasonMap (one line). content_filter was already mapped ("content-filter"); only max_output_tokens was missing. After this, both the streaming response.incomplete event handler and the non-streaming terminal resolve a budget truncation to finishReason: "length".

A changeset is included (@effect/ai-openai patch).

Notes

  • "max_output_tokens" → "length" is the semantically correct mapping in the @effect/ai FinishReason contract (both denote "ran out of output budget"); no new finish-reason variant is needed.
  • The packages/ai/openai package has no existing unit-test suite for these internal helpers, so this follows the same fix-plus-changeset shape as comparable small provider-adapter corrections.

The OpenAI Responses API reports a budget/output-length truncation as
response.incomplete with incomplete_details.reason "max_output_tokens"
(distinct from Chat Completions finish_reason "length"). That key was missing
from finishReasonMap, so resolveFinishReason fell through to "unknown" for a
genuinely length-truncated response instead of the portable "length" finish
reason. Map max_output_tokens to "length" so a truncated turn surfaces a
meaningful finish reason. content_filter was already mapped.
@shtse8 shtse8 requested a review from IMax153 as a code owner June 29, 2026 01:59
@github-project-automation github-project-automation Bot moved this to Discussion Ongoing in PR Backlog Jun 29, 2026
@changeset-bot

changeset-bot Bot commented Jun 29, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 8d9a23c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@effect/ai-openai Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR


Map the OpenAI Responses `max_output_tokens` finish reason to `"length"`.

The OpenAI Responses API reports a budget/output-length truncation as

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we create a more succinct change here? This is overly verbose.

Then this looks good to merge

@github-project-automation github-project-automation Bot moved this from Discussion Ongoing to Waiting on Author in PR Backlog Jun 29, 2026
@shtse8

shtse8 commented Jun 29, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review! Shortened the changeset to a single line.

@IMax153 IMax153 merged commit 3e59443 into Effect-TS:main Jun 29, 2026
10 of 11 checks passed
@github-project-automation github-project-automation Bot moved this from Waiting on Author to Done in PR Backlog Jun 29, 2026
@github-actions github-actions Bot mentioned this pull request Jun 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants