Skip to content

fix(core): keep usage and finish reason from AI SDK V2 providers - #50797

Closed
Veld101 wants to merge 1 commit into
anomalyco:devfrom
Veld101:v2-provider-usage
Closed

Veld101 wants to merge 1 commit into
anomalyco:devfrom
Veld101:v2-provider-usage

Conversation

@Veld101

@Veld101 Veld101 commented Sep 23, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #50338

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Dynamically loaded providers that implement AI SDK LanguageModelV2 silently lose token usage, which also disables automatic compaction.

packages/core/src/aisdk.ts calls language.doStream() directly and reads the result as LanguageModelV3StreamPart. It reads usage.inputTokens.total, usage.outputTokens.total, usage.outputTokens.reasoning and finishReason.unified. A V2 provider reports flat counts ({ inputTokens: 82057, outputTokens: 4, totalTokens: 82061 }) and a bare reason ("stop"), so every one of those reads was undefined. Usage reached the session as zero and SessionCompaction.required never saw the session approach the model's limit.

The fix normalizes both shapes where provider stream parts enter:

const inputTokens = tokenTotal(input.inputTokens)   // number (V2) or { total } (V3)
const outputTokens = tokenTotal(input.outputTokens)
...
const unified = typeof value === "string" ? value : value.unified

Non-finish events are already shape-compatible — the AI SDK's own V2-to-V3 converter only rewrites finish — so nothing else changes.

asLanguageModelV3 in the ai package would do the same conversion, but it is not exported, so the normalization lives at the parse boundary instead.

How did you verify your code works?

New test keeps usage and finish reason from a LanguageModelV2 provider: a V2-shaped model (specificationVersion: "v2") reports inputTokens: 82_057, outputTokens: 4, totalTokens: 82_061 and finishReason: "stop". The response keeps finishReason: { normalized: "stop", raw: "stop" } and the token counts. Without the fix the test fails on finish-reason schema validation because normalized is undefined.

bun test test/aisdk.test.ts   # 40 pass, 0 fail
bun typecheck                 # packages/core, clean
bun run lint                  # clean

The full packages/core suite is 4736 pass / 27 fail. The failures reproduce on unmodified v2 — I verified a sample with this change stashed — and are Windows environment issues (shell, git worktrees, config discovery), unrelated to this change.

A V1 variant of the same fix is open as #50540. This PR is the v2 change for the report.

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

Based on the search results, I found one related PR:

#50540 - fix(session): preserve reported usage for AI SDK V2 providers

Why it's related: This PR is explicitly mentioned in the description of PR #50797 as "A V1 variant of the same fix is open as #50540. This PR is the v2 change for the report." These are complementary fixes for the same issue (preserving token usage and finish reason from AI SDK V2 providers), with #50540 targeting the v1 branch and #50797 targeting the v2 branch.

@ton618to

Copy link
Copy Markdown

Confirmed the exact V2 finish-event failure this PR describes in a compiled OpenCode 2.0.31-based build with SAP AI Core. This is user-visible session interruption, not only missing usage: text streams, then LLMEvent.stepFinish rejects reason.normalized and the Session drain stops. A plugin-free production-bridge probe fails with V2 finish events and succeeds with V3 events. Full sanitized evidence and environment: #50338 (comment). Please prioritize review of the V2 compatibility fix. I have not applied or tested this PR locally.

@Veld101

Veld101 commented Oct 20, 2026

Copy link
Copy Markdown
Author

Thanks for the independent confirmation and the sanitized evidence, @ton618to. That matches the failure this PR targets: the V2 finish event rejects reason.normalized, so the step never settles and the drain stops. This change keeps the reported usage and finish reason from V2 providers; it has no conflicts against current dev and CI is green, waiting on review.

@github-actions

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: AI SDK V2 providers silently lose usage and disable auto-compaction

2 participants