Loading...
Generate OpenAI strict json_schema, Anthropic tool schemas, and Gemini response schemas from JSON or interfaces
Load real-world extraction payloads and strict schema definitions
Paste sample JSON or a draft schema to enforce strict compliance
{
"type": "object",
"properties": {
"vendorName": {
"type": "string"
},
"invoiceNumber": {
"type": "string"
},
"date": {
"type": "string"
},
"totalAmount": {
"type": "number"
},
"currency": {
"type": "string"
},
"lineItems": {
"type": "array",
"items": {
"type": "object",
"properties": {
"description": {
"type": "string"
},
"quantity": {
"type": "integer"
},
"unitPrice": {
"type": "integer"
}
},
"required": [
"description",
"quantity",
"unitPrice"
],
"additionalProperties": false
}
},
"isPaid": {
"type": "boolean"
}
},
"required": [
"vendorName",
"invoiceNumber",
"date",
"totalAmount",
"currency",
"lineItems",
"isPaid"
],
"additionalProperties": false
}import OpenAI from 'openai';
const client = new OpenAI();
// Schema definition with strict: true
const InvoiceExtractionSchema = {
name: 'InvoiceExtraction',
strict: true,
schema: {
"type": "object",
"properties": {
"vendorName": {
"type": "string"
},
"invoiceNumber": {
"type": "string"
},
"date": {
"type": "string"
},
"totalAmount": {
"type": "number"
},
"currency": {
"type": "string"
},
"lineItems": {
"type": "array",
"items": {
"type": "object",
"properties": {
"description": {
"type": "string"
},
"quantity": {
"type": "integer"
},
"unitPrice": {
"type": "integer"
}
},
"required": [
"description",
"quantity",
"unitPrice"
],
"additionalProperties": false
}
},
"isPaid": {
"type": "boolean"
}
},
"required": [
"vendorName",
"invoiceNumber",
"date",
"totalAmount",
"currency",
"lineItems",
"isPaid"
],
"additionalProperties": false
}
};
async function main() {
const response = await client.chat.completions.create({
model: 'gpt-4o-2024-08-06',
messages: [
{ role: 'system', content: 'Extract structured information.' },
{ role: 'user', content: 'Process input...' }
],
response_format: {
type: 'json_schema',
json_schema: InvoiceExtractionSchema
}
});
const parsed = JSON.parse(response.choices[0].message.content || '{}');
console.log(parsed);
}
main();import json
from openai import OpenAI
client = OpenAI()
schema = {
"name": "InvoiceExtraction",
"strict": True,
"schema": {
"type": "object",
"properties": {
"vendorName": {
"type": "string"
},
"invoiceNumber": {
"type": "string"
},
"date": {
"type": "string"
},
"totalAmount": {
"type": "number"
},
"currency": {
"type": "string"
},
"lineItems": {
"type": "array",
"items": {
"type": "object",
"properties": {
"description": {
"type": "string"
},
"quantity": {
"type": "integer"
},
"unitPrice": {
"type": "integer"
}
},
"required": [
"description",
"quantity",
"unitPrice"
],
"additionalProperties": False
}
},
"isPaid": {
"type": "boolean"
}
},
"required": [
"vendorName",
"invoiceNumber",
"date",
"totalAmount",
"currency",
"lineItems",
"isPaid"
],
"additionalProperties": False
}
}
response = client.chat.completions.create(
model="gpt-4o-2024-08-06",
messages=[
{"role": "system", "content": "Extract structured information."},
{"role": "user", "content": "Process input..."}
],
response_format={"type": "json_schema", "json_schema": schema}
)
data = json.loads(response.choices[0].message.content)
print(data)import Anthropic from '@anthropic-ai/sdk';
const anthropic = new Anthropic();
const response = await anthropic.messages.create({
model: 'claude-3-5-sonnet-20241022',
max_tokens: 1024,
tools: [
{
name: 'InvoiceExtraction',
description: 'Strict structured output definition',
input_schema: {
"type": "object",
"properties": {
"vendorName": {
"type": "string"
},
"invoiceNumber": {
"type": "string"
},
"date": {
"type": "string"
},
"totalAmount": {
"type": "number"
},
"currency": {
"type": "string"
},
"lineItems": {
"type": "array",
"items": {
"type": "object",
"properties": {
"description": {
"type": "string"
},
"quantity": {
"type": "integer"
},
"unitPrice": {
"type": "integer"
}
},
"required": [
"description",
"quantity",
"unitPrice"
],
"additionalProperties": false
}
},
"isPaid": {
"type": "boolean"
}
},
"required": [
"vendorName",
"invoiceNumber",
"date",
"totalAmount",
"currency",
"lineItems",
"isPaid"
],
"additionalProperties": false
}
}
],
tool_choice: { type: 'tool', name: 'InvoiceExtraction' },
messages: [{ role: 'user', content: 'Process input...' }]
});
const toolUse = response.content.find(block => block.type === 'tool_use');
console.log(toolUse?.input);import { GoogleGenerativeAI, SchemaType } from '@google/generative-ai';
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY!);
const model = genAI.getGenerativeModel({
model: 'gemini-1.5-pro',
generationConfig: {
responseMimeType: 'application/json',
responseSchema: {
"type": "object",
"properties": {
"vendorName": {
"type": "string"
},
"invoiceNumber": {
"type": "string"
},
"date": {
"type": "string"
},
"totalAmount": {
"type": "number"
},
"currency": {
"type": "string"
},
"lineItems": {
"type": "array",
"items": {
"type": "object",
"properties": {
"description": {
"type": "string"
},
"quantity": {
"type": "integer"
},
"unitPrice": {
"type": "integer"
}
},
"required": [
"description",
"quantity",
"unitPrice"
],
"additionalProperties": false
}
},
"isPaid": {
"type": "boolean"
}
},
"required": [
"vendorName",
"invoiceNumber",
"date",
"totalAmount",
"currency",
"lineItems",
"isPaid"
],
"additionalProperties": false
}
}
});
const result = await model.generateContent('Process input...');
console.log(JSON.parse(result.response.text()));