fix(openai-base): preserve caller-supplied runId in chat completions adapter #2193
main fromrefs/pull/2193/head·merged by tombeckenham View on GitHubChat Completions ignored the runId that you pass to chat(). Each model call made a new id. A later RUN_FINISHED named a run that never started. This change keeps that runId on RUN_STARTED and on RUN_FINISHED.
🎯 Changes#
OpenAIBaseChatCompletionsTextAdapter now uses a caller runId in chatStream and in structuredOutputStream. When you omit runId, the adapter still creates one. This matches the Responses adapter and the Anthropic adapter.
No docs page described the old behavior. The public contract already said that a caller runId is the run id. Docs stay as they are.
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested code changes locally with
pnpm run test:pr, or these tests do not apply to this pull request. - I fully understand the code in this pull request, including any code generated with AI assistance.
- Docs: I updated
docs/for this change, or this change is not user-facing. - Changeset: I added a changeset (
pnpm changeset), or this PR does not change a published package.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Root cause#
Issue. A caller who passes runId to chat() with openaiCompatibleText never sees that id on RUN_STARTED or RUN_FINISHED.
Cause. OpenAIBaseChatCompletionsTextAdapter.chatStream and structuredOutputStream always called generateId.
Fix. Those two methods use the caller runId when it is present. They still call generateId when it is absent.
Possible alternatives#
- Rewrite the id in
chat(). The activity would replacerunIdon each event. That misses a caller of the adapter. Responses and Anthropic do not do this. - Change only
chatStream.structuredOutputStreamwould still drop the id.chat()passesrunIdinto that path.
Testing#
Commands run. An agent-written vitest called chatStream with runId: 'run-from-client'. On main (16026d91a) it failed. The events were RUN_STARTED:openai-compatible-1795469024470-28hrj8vspk6 and RUN_FINISHED:openai-compatible-1795469024470-28hrj8vspk6. The same test passed on the fix. pnpm test:pr was not run. This push only adds the changeset.
Manual test.
- On
main, call the Chat CompletionschatStreamwithrunId: 'run-from-client'.RUN_STARTEDshowsopenai-compatible-<time>-<random>. - On this branch, run the unit tests named
preserves caller-supplied runId.RUN_STARTEDandRUN_FINISHEDshowrun-from-client. - Call
chatStreamwith norunId.RUN_STARTEDstill has a generated id.
How this PR makes testing easy. packages/openai-base/tests/chat-completions-text.test.ts and packages/openai-base/tests/chat-completions-structured-output-stream.test.ts.
Linked issues#
Closes #2115
Risk / rollback#
Risk is low. Callers who omit runId see no change. Revert the pull request to restore the old ids.
Public API change#
The chat() call does not change. The runId on the events does.
Before
chat({
adapter: openaiCompatibleText('deepseek-chat', { apiKey, baseURL }),
runId: 'run-from-client',
messages,
})
// RUN_FINISHED.runId is a generated id
After
chat({
adapter: openaiCompatibleText('deepseek-chat', { apiKey, baseURL }),
runId: 'run-from-client',
messages,
})
// RUN_FINISHED.runId is 'run-from-client'
Summary by CodeRabbit#
- Bug Fixes
- Streaming chat and structured-output responses now preserve a caller-provided run ID across start and finish events, including after a tool loop. When no run ID is provided, one is generated. This keeps the same identifier available throughout the stream lifecycle for both response types.
tombeckenham approved these changes
Thanks for the fix. The two Chat Completions streams now keep a caller runId, the tests cover both paths, and I rebased the branch onto main so CI runs the current tree.