Public preview: This API is in public preview. Endpoints, schemas, and limits may change before general availability.
Using Structured Outputs
Get JSON that follows your schema by passing response_format, with a plain JSON schema or a Pydantic model.
You can ask the API for a JSON object instead of prose by passing response_format, in the same shape as OpenAI’s Chat Completions API:
{ "type": "json_schema", "json_schema": { "name": …, "schema": …, "strict": true } }— JSON that follows your schema.{ "type": "json_object" }— any valid JSON object; describe the keys you want in your prompt.
output_text then holds the JSON as a string.
Using a JSON schema#
response_format is not part of the SDK’s types for responses.create, so pass it in extra_body (Python) or directly in the request object (JavaScript).
import json
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["EUGPT_API_KEY"],
base_url="https://api.eugpt.ai/v1",
)
response = client.responses.create(
model="auto",
input="John Doe is 34 years old and lives in New York.",
stream=False,
extra_body={
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "PersonExtraction",
"strict": True,
"schema": {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"},
"city": {"type": "string"},
},
"required": ["name", "age", "city"],
"additionalProperties": False,
},
},
}
},
)
person = json.loads(response.output_text)
print(person["name"], person["age"])import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.EUGPT_API_KEY,
baseURL: "https://api.eugpt.ai/v1",
});
const response = await client.responses.create({
model: "auto",
input: "John Doe is 34 years old and lives in New York.",
stream: false,
// Not in the SDK's types; in TypeScript, cast the request object.
response_format: {
type: "json_schema",
json_schema: {
name: "PersonExtraction",
strict: true,
schema: {
type: "object",
properties: {
name: { type: "string" },
age: { type: "integer" },
city: { type: "string" },
},
required: ["name", "age", "city"],
additionalProperties: false,
},
},
},
});
const person = JSON.parse(response.output_text);
console.log(person.name, person.age);curl https://api.eugpt.ai/v1/responses \
-H "Authorization: Bearer $EUGPT_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"input": "John Doe is 34 years old and lives in New York.",
"stream": false,
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "PersonExtraction",
"strict": true,
"schema": {
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer" },
"city": { "type": "string" }
},
"required": ["name", "age", "city"],
"additionalProperties": false
}
}
}
}'Tip: Use the
"required"array to enforce which fields must always be returned. A property that is not required and has no matching data in the input is usually left out.Tip: Set
"additionalProperties": falseto keep the model to the keys in your schema. Withtrue, the model may add keys for extra information it finds in the input.
Using Pydantic with a file#
For complex schemas, it is often easier to define your data model with Pydantic. Generate the JSON schema with .model_json_schema(), send it as response_format, and validate the answer with .model_validate_json().
This example also extracts the data straight from an uploaded document, by adding an input_file part to the input (see Using files as context).
import os
from pydantic import BaseModel, Field
from openai import OpenAI
client = OpenAI(
api_key=os.environ["EUGPT_API_KEY"],
base_url="https://api.eugpt.ai/v1",
)
# 1. Define your extraction schema with Pydantic
class QuarterlyReportExtraction(BaseModel):
company_name: str = Field(description="The name of the company issuing the report")
quarter: str = Field(description="The quarter being reported (e.g. Q1 2024)")
total_revenue: float = Field(description="The total revenue reported for the quarter")
key_highlights: list[str] = Field(
description="A list of key highlights, risks, or milestones mentioned"
)
# 2. Send the file, the question, and the schema
response = client.responses.create(
model="auto",
input=[
{
"role": "user",
"content": [
{"type": "input_file", "file_id": "<your_file_id>"},
{"type": "input_text", "text": "Extract the key figures from this report."},
],
}
],
stream=False,
extra_body={
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "QuarterlyReportExtraction",
"strict": True,
"schema": QuarterlyReportExtraction.model_json_schema(),
},
}
},
)
# 3. Validate the JSON into your Pydantic object
extracted = QuarterlyReportExtraction.model_validate_json(response.output_text)
print(extracted.company_name)
print(extracted.key_highlights)
Validating the answer is good practice in any case: if the model’s output does not fit your schema, model_validate_json raises an error you can handle, for example by retrying.