Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/lib/ResponsesParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ function parseTextFormat<
}

export function hasAutoParseableInput(params: ResponseCreateParamsWithTools): boolean {
if (isAutoParsableResponseFormat(params.text?.format)) {
if (isAutoParsableResponseFormat(params.text?.format) || params.text?.format?.type === 'json_schema') {
Comment thread
HAYDEN-OAI marked this conversation as resolved.
Outdated
return true;
}

Expand Down
5 changes: 4 additions & 1 deletion src/lib/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,10 @@ export function shouldParseToolCall(
}

export function hasAutoParseableInput(params: AnyChatCompletionCreateParams): boolean {
if (isAutoParsableResponseFormat(params.response_format)) {
if (
isAutoParsableResponseFormat(params.response_format) ||
params.response_format?.type === 'json_schema'
Comment thread
HAYDEN-OAI marked this conversation as resolved.
Outdated
) {
return true;
}

Expand Down
9 changes: 9 additions & 0 deletions tests/lib/ResponsesParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,13 @@ describe('ResponsesParser', () => {
parsed_arguments: { city: 'Paris' },
});
});

it('parses raw json_schema text format in maybeParseResponse', () => {
const response = maybeParseResponse(
makeResponse('completed', '{"size":"large","quality":"good"}'),
structuredTextParams,
);

expect(response.output_parsed).toEqual({ size: 'large', quality: 'good' });
});
});
41 changes: 41 additions & 0 deletions tests/lib/parser.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { z as z4 } from 'zod/v4';
import { z as z3 } from 'zod/v3';
import { zodResponseFormat } from 'openai/helpers/zod';
import { maybeParseChatCompletion } from '../../src/lib/parser';
import { makeSnapshotRequest } from '../utils/mock-snapshots';

jest.setTimeout(1000 * 30);
Expand Down Expand Up @@ -1406,3 +1407,43 @@ describe.each([
});
});
});

describe('maybeParseChatCompletion', () => {
it('parses raw json_schema response_format', () => {
const rawCompletion = {
id: 'chatcmpl-123',
object: 'chat.completion' as const,
created: 1677652288,
model: 'gpt-4o-2024-08-06',
choices: [
{
index: 0,
finish_reason: 'stop' as const,
logprobs: null,
message: {
role: 'assistant' as const,
content: '{"city":"San Francisco","units":"c"}',
refusal: null,
},
},
],
};

const parsed = maybeParseChatCompletion(rawCompletion, {
model: 'gpt-4o-2024-08-06',
messages: [{ role: 'user', content: 'hello' }],
response_format: {
type: 'json_schema',
json_schema: {
name: 'location',
schema: { type: 'object' },
},
},
});

expect(parsed.choices[0]?.message.parsed).toEqual({
city: 'San Francisco',
units: 'c',
});
});
});