Repository navigation
Commit 73d48d0
authored
fix(openai): correlate rotated Responses API item ids (#18548)
## Background
`@ai-sdk/openai` keyed active Responses API reasoning state and emitted
stream-part IDs by each event's opaque `item_id`. A real request to
GitHub Copilot's `/responses` endpoint with `gpt-5.3-codex` showed that
Copilot sends `response.output_item.added`, but rotates the opaque ID on
every subsequent reasoning and message event. `output_index` remains
stable.
That caused the reported `activeReasoningPart.summaryParts` crash. If
the crash was bypassed, the same rotation produced the downstream `text
part ... not found` error because text start, delta, and end used
different IDs.
The committed fixture is the complete 69-event SSE sequence captured
from the real provider on 2026-08-06. Opaque identifiers, encrypted
reasoning payloads, obfuscation padding, and the safety identifier were
sanitized; event order, event types, output indexes, deltas, usage, and
response structure were preserved.
## Summary
- Retain `output_index` when parsing text-delta and reasoning-summary
events.
- Track the canonical message/reasoning item ID from
`response.output_item.added` by `output_index`.
- Use that canonical ID for reasoning/text deltas and end events, and
avoid dereferencing absent reasoning state.
- Add the sanitized live Copilot SSE capture as a regression fixture and
assert that it emits no stream errors, preserves one ID per logical
reasoning/text part, and returns the complete answer.
- Add a public-API end-to-end example that serves the captured
ID-rotation pattern over localhost SSE.
- Add a patch changeset for `@ai-sdk/openai`.
## Contributor Credit
Thanks to @A-S-Manoj for the upstream report and diagnosis; @drmgc and
@EntropyParadigm for the Nanocoder reports; @daitmaamar4-png, @OverHash,
and @jianminLee for independent downstream reports; and
@mussaratshamsher for the related work in #18337.
## End-to-End Verification
I first sent an authenticated request to the real GitHub Copilot
Responses API using the credential created by `nanocoder copilot login`.
Replaying that exact raw response through public
`createOpenAI(...).responses(...)` and `streamText(...)` failed before
this change with:
```text
TypeError: Cannot read properties of undefined (reading 'summaryParts')
```
After rebuilding `@ai-sdk/openai`, replaying the same unsanitized raw
response completed successfully.
I also ran the checked-in localhost HTTP/SSE reproduction:
```sh
cd examples/ai-functions
pnpm tsx src/stream-text/openai/responses-reasoning-id-rotation.ts
```
It now prints:
```text
Expected: reasoning summary followed by "ok".
Thinking through the request.ok
```
This is the complete end-to-end example used for that verification:
```ts
import { createServer } from 'node:http';
import { createOpenAI } from '@ai-sdk/openai';
import { streamText } from 'ai';
import { run } from '../../lib/run';
const modelId = 'gpt-5.3-codex';
const responseId = 'resp_copilot_repro';
const reasoningAddedId = 'rs_copilot_added';
const reasoningSummaryAddedId = 'rs_copilot_summary_added';
const reasoningSummaryDeltaId = 'rs_copilot_summary_delta';
const reasoningSummaryDoneId = 'rs_copilot_summary_done';
const reasoningDoneId = 'rs_copilot_done';
const messageAddedId = 'msg_copilot_added';
const messageDeltaId = 'msg_copilot_delta';
const messageDoneId = 'msg_copilot_done';
const response = {
id: responseId,
object: 'response',
created_at: 1,
status: 'in_progress',
error: null,
incomplete_details: null,
input: [],
instructions: null,
max_output_tokens: null,
model: modelId,
output: [],
parallel_tool_calls: true,
previous_response_id: null,
reasoning: { effort: 'low', summary: 'auto' },
store: true,
temperature: null,
text: { format: { type: 'text' } },
tool_choice: 'auto',
tools: [],
top_p: null,
truncation: 'disabled',
usage: null,
user: null,
metadata: {},
};
const reasoningItem = {
id: reasoningDoneId,
type: 'reasoning',
summary: [{ type: 'summary_text', text: 'Thinking through the request.' }],
};
const messageItem = {
id: messageDoneId,
type: 'message',
status: 'completed',
role: 'assistant',
content: [{ type: 'output_text', text: 'ok', annotations: [] }],
};
const events = [
{ type: 'response.created', response },
// A live GitHub Copilot capture used a different opaque item id for every
// reasoning and message event while keeping output_index stable.
{
type: 'response.output_item.added',
output_index: 0,
item: { id: reasoningAddedId, type: 'reasoning' },
},
{
type: 'response.reasoning_summary_part.added',
item_id: reasoningSummaryAddedId,
output_index: 0,
summary_index: 0,
},
{
type: 'response.reasoning_summary_text.delta',
item_id: reasoningSummaryDeltaId,
output_index: 0,
summary_index: 0,
delta: 'Thinking through the request.',
},
{
type: 'response.reasoning_summary_part.done',
item_id: reasoningSummaryDoneId,
output_index: 0,
summary_index: 0,
},
{
type: 'response.output_item.done',
output_index: 0,
item: reasoningItem,
},
{
type: 'response.output_item.added',
output_index: 1,
item: { id: messageAddedId, type: 'message' },
},
{
type: 'response.output_text.delta',
item_id: messageDeltaId,
output_index: 1,
delta: 'ok',
},
{
type: 'response.output_item.done',
output_index: 1,
item: messageItem,
},
{
type: 'response.completed',
response: {
...response,
status: 'completed',
output: [reasoningItem, messageItem],
usage: {
input_tokens: 5,
input_tokens_details: { cached_tokens: 0 },
output_tokens: 8,
output_tokens_details: { reasoning_tokens: 7 },
total_tokens: 13,
},
},
},
];
function toSse(value: unknown) {
return `data: ${JSON.stringify(value)}\n\n`;
}
async function startMockCopilotServer() {
const server = createServer((request, response) => {
if (request.method !== 'POST' || request.url !== '/v1/responses') {
response.writeHead(404).end();
return;
}
request.resume();
request.on('end', () => {
response.writeHead(200, {
'content-type': 'text/event-stream',
connection: 'close',
});
for (const event of events) {
response.write(toSse(event));
}
response.end();
});
});
await new Promise<void>((resolve, reject) => {
server.once('error', reject);
server.listen(0, '127.0.0.1', () => {
server.off('error', reject);
resolve();
});
});
const address = server.address();
if (address == null || typeof address === 'string') {
throw new Error('Mock Copilot server did not bind to a TCP port.');
}
return {
server,
baseURL: `http://127.0.0.1:${address.port}/v1`,
};
}
run(async () => {
const { server, baseURL } = await startMockCopilotServer();
try {
const copilot = createOpenAI({
apiKey: 'test-key',
baseURL,
});
const result = streamText({
model: copilot.responses(modelId),
prompt: 'Reply with exactly: ok',
});
console.log('Expected: reasoning summary followed by "ok".');
for await (const part of result.stream) {
if (part.type === 'reasoning-delta' || part.type === 'text-delta') {
process.stdout.write(part.text);
}
}
console.log();
} finally {
await new Promise<void>((resolve, reject) => {
server.close(error => {
if (error != null) {
reject(error);
return;
}
resolve();
});
});
}
});
```
## Checklist
- [x] All commits are signed (PRs with unsigned commits cannot be
merged)
- [x] Tests have been added / updated (for bug fixes / features)
- [x] Documentation has been added / updated (for bug fixes / features)
- [x] A _patch_ changeset for relevant packages has been added (for bug
fixes / features - run `pnpm changeset` in the project root)
- [x] I have reviewed this pull request (self-review)
## Future Work
#18337 proposes synthesizing `reasoning-start` when a provider truly
omits `response.output_item.added`. The real Copilot trace includes that
event with a different ID, so this PR keeps that separate behavior out
of scope. It can be evaluated independently if a raw provider trace
confirms the missing-event variant.
## Related Issues
Fixes #18270
- Nano-Collective/nanocoder#719
- Nano-Collective/nanocoder#576
- anomalyco/opencode#21346
- Soju06/codex-lb#398
- cita-777/metapi#269
- Related implementation: #183371 parent e6415bd commit 73d48d0
6 files changed
Lines changed: 471 additions & 93 deletions
File tree
- .changeset
- examples/ai-functions/src/stream-text/openai
- packages/openai/src/responses
- __fixtures__
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
0 commit comments