Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/warm-bees-reason.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ai-sdk/open-responses': patch
---

Preserve assistant reasoning when replaying Open Responses output in tool loops.
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import assert from 'node:assert/strict';
import { createOpenResponses } from '@ai-sdk/open-responses';
import type { LanguageModelV4Prompt } from '@ai-sdk/provider';

const reasoningText = 'REASONING THAT MUST SURVIVE THE ROUND TRIP';

type AssistantContent = Extract<
LanguageModelV4Prompt[number],
{ role: 'assistant' }
>['content'];

type RequestItem = {
type?: string;
role?: string;
call_id?: string;
content?: unknown;
};

type RequestBody = {
input: RequestItem[];
};

async function main() {
const requestBodies: RequestBody[] = [];

function modelReturning(output: unknown[]) {
return createOpenResponses({
name: 'reproduction',
apiKey: 'not-used',
url: 'https://example.invalid/v1/responses',
fetch: async (_url, init) => {
requestBodies.push(JSON.parse(String(init?.body)) as RequestBody);

return new Response(
JSON.stringify({
id: 'r',
object: 'response',
created_at: 0,
model: 'm',
status: 'completed',
output,
usage: { input_tokens: 1, output_tokens: 1 },
}),
{
status: 200,
headers: { 'content-type': 'application/json' },
},
);
},
})('any-model');
}

const firstStep = await modelReturning([
{
type: 'reasoning',
id: 'rs_1',
content: [{ type: 'reasoning_text', text: reasoningText }],
summary: [],
},
{
type: 'function_call',
id: 'fc_1',
call_id: 'call_1',
name: 'get_weather',
arguments: '{"location":"San Francisco"}',
},
]).doGenerate({
prompt: [
{
role: 'user',
content: [{ type: 'text', text: 'What is the weather?' }],
},
],
tools: [
{
type: 'function',
name: 'get_weather',
inputSchema: {
type: 'object',
properties: { location: { type: 'string' } },
required: ['location'],
},
},
],
});

const reasoningRead = firstStep.content.filter(
part => part.type === 'reasoning',
);
assert.equal(
reasoningRead.length,
1,
'precondition: the provider response should produce one reasoning part',
);

await modelReturning([]).doGenerate({
prompt: [
{
role: 'user',
content: [{ type: 'text', text: 'What is the weather?' }],
},
{
role: 'assistant',
content: firstStep.content as AssistantContent,
},
{
role: 'tool',
content: [
{
type: 'tool-result',
toolCallId: 'call_1',
toolName: 'get_weather',
output: {
type: 'json',
value: { temperature: 72, condition: 'sunny' },
},
},
],
},
],
tools: [
{
type: 'function',
name: 'get_weather',
inputSchema: {
type: 'object',
properties: { location: { type: 'string' } },
required: ['location'],
},
},
],
});

const secondRequest = requestBodies.at(-1);
assert.ok(secondRequest != null, 'no second request body was captured');

assert.deepEqual(
secondRequest.input.map(item => item.type ?? `message:${item.role}`),
['message', 'reasoning', 'function_call', 'function_call_output'],
);

const replayedReasoning = secondRequest.input[1];
assert.deepEqual(replayedReasoning, {
type: 'reasoning',
summary: [],
content: [{ type: 'reasoning_text', text: reasoningText }],
});

assert.equal(secondRequest.input[2].call_id, 'call_1');
assert.equal(secondRequest.input[3].call_id, 'call_1');

console.log('Reasoning survived the Open Responses tool-loop round trip.');
console.log(
secondRequest.input
.map(item => item.type ?? `message:${item.role}`)
.join(', '),
);
}

main().catch(error => {
console.error(error);
process.exitCode = 1;
});
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,66 @@ describe('convertToOpenResponsesInput', () => {
});

describe('assistant messages with tool calls', () => {
it('should preserve reasoning before a tool call and its result', async () => {
const result = await convertToOpenResponsesInput({
prompt: [
{
role: 'assistant',
content: [
{
type: 'reasoning',
text: 'I should use the weather tool.',
},
{
type: 'tool-call',
toolCallId: 'call_123',
toolName: 'get_weather',
input: { location: 'San Francisco' },
},
],
},
{
role: 'tool',
content: [
{
type: 'tool-result',
toolCallId: 'call_123',
toolName: 'get_weather',
output: {
type: 'json',
value: { temperature: 72, condition: 'sunny' },
},
},
],
},
],
});

expect(result.input).toEqual([
{
type: 'reasoning',
summary: [],
content: [
{
type: 'reasoning_text',
text: 'I should use the weather tool.',
},
],
},
{
type: 'function_call',
call_id: 'call_123',
name: 'get_weather',
arguments: '{"location":"San Francisco"}',
},
{
type: 'function_call_output',
call_id: 'call_123',
output: '{"temperature":72,"condition":"sunny"}',
},
]);
});

it('should convert assistant message with a single tool-call', async () => {
const result = await convertToOpenResponsesInput({
prompt: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
InputTextContentParam,
OpenResponsesRequestBody,
OutputTextContentParam,
ReasoningItemParam,
RefusalContentParam,
} from './open-responses-api';

Expand Down Expand Up @@ -105,10 +106,19 @@ export async function convertToOpenResponsesInput({
const assistantContent: Array<
OutputTextContentParam | RefusalContentParam
> = [];
const reasoningItems: Array<ReasoningItemParam> = [];
const toolCalls: Array<FunctionCallItemParam> = [];

for (const part of content) {
switch (part.type) {
case 'reasoning': {
reasoningItems.push({
type: 'reasoning',
summary: [],
content: [{ type: 'reasoning_text', text: part.text }],
});
break;
}
case 'text': {
assistantContent.push({ type: 'output_text', text: part.text });
break;
Expand All @@ -129,6 +139,11 @@ export async function convertToOpenResponsesInput({
}
}

// Push reasoning as separate items
for (const reasoningItem of reasoningItems) {
input.push(reasoningItem);
}

// Push assistant message with text content if any
if (assistantContent.length > 0) {
input.push({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export type ReasoningItemParam = {
id?: string;
type: 'reasoning';
summary: ReasoningSummaryContentParam[];
content?: unknown;
content?: ReasoningTextContent[];
encrypted_content?: string;
};

Expand Down