OpenAI Responses Structured Outputs with AI Gateway
Use text.format to constrain the model's output to a JSON schema:
structured.ts
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: process.env.AI_GATEWAY_API_KEY,
baseURL: 'https://ai-gateway.vercel.sh/v1',
});
const response = await client.responses.create({
model: 'openai/gpt-6-astra',
input: 'List 3 colors with their hex codes.',
text: {
format: {
type: 'json_schema',
name: 'colors',
strict: true,
schema: {
type: 'object',
properties: {
colors: {
type: 'array',
items: {
type: 'object',
properties: {
name: { type: 'string' },
hex: { type: 'string' },
},
required: ['name', 'hex'],
additionalProperties: false,
},
},
},
required: ['colors'],
additionalProperties: false,
},
},
},
});
const data = JSON.parse(response.output_text);
console.log(data.colors);structured.py
import os
import json
from openai import OpenAI
client = OpenAI(
api_key=os.getenv('AI_GATEWAY_API_KEY'),
base_url='https://ai-gateway.vercel.sh/v1',
)
response = client.responses.create(
model='openai/gpt-6-astra',
input='List 3 colors with their hex codes.',
text={
'format': {
'type': 'json_schema',
'name': 'colors',
'strict': True,
'schema': {
'type': 'object',
'properties': {
'colors': {
'type': 'array',
'items': {
'type': 'object',
'properties': {
'name': {'type': 'string'},
'hex': {'type': 'string'},
},
'required': ['name', 'hex'],
'additionalProperties': False,
},
},
},
'required': ['colors'],
'additionalProperties': False,
},
},
},
)
data = json.loads(response.output_text)
print(data['colors'])structured-output.sh
curl -X POST "https://ai-gateway.vercel.sh/v1/responses" \
-H "Authorization: Bearer $AI_GATEWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "openai/gpt-6-astra",
"input": "List 3 colors with their hex codes.",
"text": {
"format": {
"type": "json_schema",
"name": "colors",
"strict": true,
"schema": {
"type": "object",
"properties": {
"colors": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"hex": {
"type": "string"
}
},
"required": [
"name",
"hex"
],
"additionalProperties": false
}
}
},
"required": [
"colors"
],
"additionalProperties": false
}
}
}
}'Last updated September 8, 2026
Was this helpful?