Skip to content
Open
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/flat-carpets-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ai-sdk/openai": patch
---

Fix Responses API history serialization for function tools named `tool_search`.
Original file line number Diff line number Diff line change
Expand Up @@ -1316,6 +1316,62 @@ describe('convertToOpenAIResponsesInput', () => {
]);
});

it('should convert a function named tool_search as a function call and output', async () => {
const result = await convertToOpenAIResponsesInput({
toolNameMapping: testToolNameMapping,
hasToolSearchTool: false,
prompt: [
{
role: 'assistant',
content: [
{
type: 'tool-call',
toolCallId: 'call_123',
toolName: 'tool_search',
input: {
query: 'synthetic query',
limit: 10,
},
},
],
},
{
role: 'tool',
content: [
{
type: 'tool-result',
toolCallId: 'call_123',
toolName: 'tool_search',
output: {
type: 'json',
value: { tools: [] },
},
},
],
},
],
systemMessageMode: 'system',
providerOptionsName: 'openai',
store: true,
});

expect(result.input).toMatchInlineSnapshot(`
[
{
"arguments": "{"query":"synthetic query","limit":10}",
"call_id": "call_123",
"name": "tool_search",
"type": "function_call",
},
{
"call_id": "call_123",
"output": "{"tools":[]}",
"type": "function_call_output",
},
]
`);
});

it('should default missing tool call input to an empty object', async () => {
const result = await convertToOpenAIResponsesInput({
toolNameMapping: testToolNameMapping,
Expand Down
15 changes: 12 additions & 3 deletions packages/openai/src/responses/convert-to-openai-responses-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export async function convertToOpenAIResponsesInput({
hasShellTool = false,
hasApplyPatchTool = false,
hasComputerTool = false,
hasToolSearchTool = true,
customProviderToolNames,
}: {
prompt: LanguageModelV4Prompt;
Expand All @@ -95,6 +96,7 @@ export async function convertToOpenAIResponsesInput({
hasShellTool?: boolean;
hasApplyPatchTool?: boolean;
hasComputerTool?: boolean;
hasToolSearchTool?: boolean;
customProviderToolNames?: Set<string>;
}): Promise<{
input: OpenAIResponsesInput;
Expand Down Expand Up @@ -359,7 +361,7 @@ export async function convertToOpenAIResponsesInput({
part.toolName,
);

if (resolvedToolName === 'tool_search') {
if (hasToolSearchTool && resolvedToolName === 'tool_search') {
if (store && id != null) {
input.push({ type: 'item_reference', id });
break;
Expand Down Expand Up @@ -580,7 +582,10 @@ export async function convertToOpenAIResponsesInput({
part.toolName,
);

if (resolvedResultToolName === 'tool_search') {
if (
hasToolSearchTool &&
resolvedResultToolName === 'tool_search'
) {
const itemId =
(
part.providerOptions?.[providerOptionsName] as
Expand Down Expand Up @@ -841,7 +846,11 @@ export async function convertToOpenAIResponsesInput({
part.toolName,
);

if (resolvedToolName === 'tool_search' && output.type === 'json') {
if (
hasToolSearchTool &&
resolvedToolName === 'tool_search' &&
output.type === 'json'
) {
const parsedOutput = await validateTypes({
value: output.value,
schema: toolSearchOutputSchema,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ export class OpenAIResponsesLanguageModel implements LanguageModelV4 {
hasShellTool: hasOpenAITool('openai.shell'),
hasApplyPatchTool: hasOpenAITool('openai.apply_patch'),
hasComputerTool: hasOpenAITool('openai.computer'),
hasToolSearchTool: hasOpenAITool('openai.tool_search'),
customProviderToolNames:
customProviderToolNames.size > 0
? customProviderToolNames
Expand Down