Skip to content

Conversation

@Mark-Life
Copy link
Contributor

Fix reasoning token streaming for gpt-5-mini and gpt-5-nano models (AG-UI)

Fixes #92 (applied to ag-ui branch)

This PR applies the reasoning streaming fix from #94 to the ag-ui branch, adapting it to use AG-UI event format instead of legacy chunk types.

🎯 Changes

Type definitions:

  • Added OpenAIReasoningOptions to gpt-5-mini and gpt-5-nano model type definitions in model-meta.ts
  • Fixed summary option placement in OpenAIReasoningOptions - moved inside reasoning object to match OpenAI SDK structure
  • Added OpenAIReasoningOptionsWithConcise interface for computer-use-preview model (supports concise summary option)

Runtime streaming:

  • Added handler for response.reasoning_summary_text.delta events in openai-adapter.ts to stream reasoning summaries
  • Uses AG-UI event format: Emits STEP_STARTED and STEP_FINISHED events instead of legacy thinking chunks
  • Reuses existing hasEmittedStepStarted flag and stepId tracking for consistent lifecycle management

Tool result serialization (⚠️ Additional change - requires careful review):

  • Added result serialization in handleToolCallEndEvent (chat.ts:313-331) to ensure tool results are consistently serialized to strings before emitting events
  • Updated test in ai-chat.test.ts:2416 to expect serialized string format instead of raw object
  • Note: This change was not part of the original issue fix (Bug: gpt-5-nano and gpt-5-mini missing OpenAIReasoningOptions in type definitions #92) but was necessary to fix a test failure. The serialization ensures consistent event format matching the legacy tool_result chunk behavior. Please review carefully as it affects tool result event emission.

Tests:

  • Updated model-meta.test.ts to expect reasoning support for gpt-5-mini, gpt-5-nano, and computer-use-preview
  • Fixed test expectations to match AG-UI event format requirements

Docs:

  • Updated docs/adapters/openai.md to include gpt-5-mini, gpt-5-nano, and computer-use-preview in supported reasoning models
  • Updated reasoning example to show summary option inside reasoning object

🔄 Differences from Original PR (#94)

  • Event format: Uses AG-UI STEP_STARTED/STEP_FINISHED events instead of legacy thinking chunks
  • Additional serialization: Includes tool result serialization fix (see note above)
  • Model support: Also adds OpenAIReasoningOptionsWithConcise for computer-use-preview

✅ Checklist

  • I have tested this code locally with pnpm run test:pr
  • I have tested this code locally with pnpm run test:lib

🚀 Release Impact

  • This change affects published code, and I have generated a changeset
  • This change is docs/CI/dev-only (no release)

⚠️ Review Notes

Please pay special attention to:

  1. Tool result serialization (chat.ts:313-331): This change serializes tool results to strings before emitting events. While this matches legacy behavior and fixes test failures, it's outside the original issue scope and should be reviewed carefully for any edge cases or breaking changes.

  2. AG-UI event format: All reasoning events now use STEP_STARTED/STEP_FINISHED format, ensuring compatibility with AG-UI protocol while maintaining backward compatibility through legacy chunk type support.

Mark-Life and others added 2 commits December 7, 2025 16:52
- Added OpenAIReasoningOptions to gpt-5-mini and gpt-5-nano models
- Fixed summary option placement (inside reasoning object)
- Added handler for response.reasoning_summary_text.delta events
- Emits AG-UI STEP_STARTED/STEP_FINISHED events for reasoning summaries
- Added OpenAIReasoningOptionsWithConcise for computer-use-preview model
- Serialize tool result to string in handleToolCallEndEvent for consistent event format
- Updated tests to match AG-UI event format requirements
@nx-cloud
Copy link

nx-cloud bot commented Dec 8, 2025

View your CI Pipeline Execution ↗ for commit d47deb7

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 1m 29s View ↗
nx run-many --targets=build --exclude=examples/** ✅ Succeeded 34s View ↗

☁️ Nx Cloud last updated this comment at 2025-12-09 11:14:29 UTC

@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 8, 2025

Open in StackBlitz

@tanstack/ai

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai@115

@tanstack/ai-anthropic

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-anthropic@115

@tanstack/ai-client

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-client@115

@tanstack/ai-devtools-core

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-devtools-core@115

@tanstack/ai-gemini

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-gemini@115

@tanstack/ai-ollama

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-ollama@115

@tanstack/ai-openai

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-openai@115

@tanstack/ai-react

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-react@115

@tanstack/ai-react-ui

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-react-ui@115

@tanstack/ai-solid

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-solid@115

@tanstack/ai-solid-ui

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-solid-ui@115

@tanstack/ai-vue

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-vue@115

@tanstack/ai-vue-ui

npm i https://pkg.pr.new/TanStack/ai/@tanstack/ai-vue-ui@115

@tanstack/react-ai-devtools

npm i https://pkg.pr.new/TanStack/ai/@tanstack/react-ai-devtools@115

@tanstack/solid-ai-devtools

npm i https://pkg.pr.new/TanStack/ai/@tanstack/solid-ai-devtools@115

commit: d47deb7

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