Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix(ai-gateway): accept nested json_schema (Sentry 7642460955) (#6873)

Ezra Ellette28da95884640
diff --git a/packages/ai-gateway/src/providers/openai.ts b/packages/ai-gateway/src/providers/openai.ts
index 661d65c5d..9eaf70a97 100644
--- a/packages/ai-gateway/src/providers/openai.ts
+++ b/packages/ai-gateway/src/providers/openai.ts
@@ -270,11 +270,15 @@ export class OpenAIProvider implements AIProvider {
switch (format.type) {
case 'json_object':
return { type: 'json_object' };
- case 'json_schema':
- if (!format.schema || !format.name) {
+ case 'json_schema': {
+ const schema = format.json_schema?.schema ?? format.schema;
+ const name = format.json_schema?.name ?? format.name;
+ const description = format.json_schema?.description ?? format.description;
+ if (!schema || !name) {
throw new Error('Schema and name are required for json_schema response format');
}
- return this.createJSONSchemaFormat(format.schema, format.name, format.description);
+ return this.createJSONSchemaFormat(schema, name, description);
+ }
default:
return undefined;
}
diff --git a/packages/ai-gateway/src/providers/openrouter.ts b/packages/ai-gateway/src/providers/openrouter.ts
index 3b9197c01..0764a2464 100644
--- a/packages/ai-gateway/src/providers/openrouter.ts
+++ b/packages/ai-gateway/src/providers/openrouter.ts
@@ -50,11 +50,15 @@ export class OpenRouterProvider implements AIProvider {
switch (format.type) {
case 'json_object':
return { type: 'json_object' };
- case 'json_schema':
- if (!format.schema || !format.name) {
+ case 'json_schema': {
+ const schema = format.json_schema?.schema ?? format.schema;
+ const name = format.json_schema?.name ?? format.name;
+ const description = format.json_schema?.description ?? format.description;
+ if (!schema || !name) {
throw new Error('Schema and name are required for json_schema response format');
}
- return this.createJSONSchemaFormat(format.schema, format.name, format.description);
+ return this.createJSONSchemaFormat(schema, name, description);
+ }
default:
return undefined;
}
diff --git a/packages/ai-gateway/src/test/openai-models.unit.test.ts b/packages/ai-gateway/src/test/openai-models.unit.test.ts
index 44d853f71..43d81b285 100644
--- a/packages/ai-gateway/src/test/openai-models.unit.test.ts
+++ b/packages/ai-gateway/src/test/openai-models.unit.test.ts
@@ -303,6 +303,32 @@ describe('OpenAI API accounting and routing', () => {
expect(params['max_tokens']).toBeUndefined();
});
+ it('accepts nested and flattened json_schema response formats', async () => {
+ const formats = [
+ { type: 'json_schema', json_schema: { name: 'answer', schema: { type: 'object' } } },
+ { type: 'json_schema', name: 'answer', schema: { type: 'object' } },
+ ];
+ for (const response_format of formats) {
+ const provider = new OpenAIProvider('sk-test') as any;
+ let capturedParams: any;
+ provider.client.chat.completions.create = mock(async (params: any) => {
+ capturedParams = params;
+ return { choices: [{ message: { content: 'ok' } }] };
+ });
+ await provider.createCompletion({
+ model: 'gpt-5.6-sol',
+ messages: [{ role: 'user', content: 'hello' }],
+ response_format,
+ } as any);
+ expect(capturedParams.response_format.json_schema).toEqual({
+ name: 'answer',
+ description: undefined,
+ schema: { type: 'object' },
+ strict: true,
+ });
+ }
+ });
+
it('adds stable GPT-5.6 explicit prompt-cache fields at the last leading system block', async () => {
const params: any = {
model: 'gpt-5.6-luna',
diff --git a/packages/ai-gateway/src/types.ts b/packages/ai-gateway/src/types.ts
index 64fe75c60..0353f2958 100644
--- a/packages/ai-gateway/src/types.ts
+++ b/packages/ai-gateway/src/types.ts
@@ -112,6 +112,11 @@ export interface ResponseFormat {
schema?: InputSchema;
name?: string;
description?: string;
+ json_schema?: {
+ schema: InputSchema;
+ name: string;
+ description?: string;
+ };
}
export interface ImageContent {