Structured responses
Request structured JSON responses when you analyze video content. This feature returns predictable, machine-readable data that integrates directly into your applications.
Key features:
- Control response structure: Define exactly how you want your video analysis results formatted using JSON schemas.
- Process responses programmatically: Eliminate manual parsing by receiving data in predictable, machine-readable formats.
- Validate data automatically: Ensure response fields meet your requirements with built-in type and format validation.
- Stream structured content: Retrieve real-time JSON responses as analysis progresses without waiting for completion.
- Integrate seamlessly: Connect video analysis directly into your existing data pipelines and applications.
Use cases:
- Directly populate data into databases: Store video analysis results in structured tables without manual formatting.
- Create automated reports: Generate consistent video summaries and insights for business intelligence dashboards.
- Build content recommendation systems: Extract keywords and topics to power personalized content suggestions.
- Populate content management systems: Auto-generate video titles, descriptions, and tags in your CMS workflow.
- Facilitate search and filtering: Organize video metadata to make content discoverable across your platform
Control responses
Use prompts and schemas together to control your responses:
- The prompt specifies what the platform must extract from the video.
- The schema defines the structure, field names, and data types for the response.
The schema takes precedence over the prompt. If your prompt requests a summary but your schema defines chapters, the platform returns chapters. Always ensure your prompt and schema align to request the same type of information.
Choose your schema
A schema defines:
- Field names: The properties that appear in the JSON response.
- Data types: The type of each field (example:
string,number, orobject). - Required fields: Fields that must be present in every response.
- Nested structures: Complex data using object definitions and references.
For complete specifications, see the JSON schema requirements section.
Prerequisites
- You already know how to analyze videos and generate text based on their content, and you’ve already indexed at least one video. For instructions, see the Analyze videos page.
Examples
The examples in this section show different types of structured responses you can request.
Summaries
Chapters
Keywords
This example generates a comprehensive video summary using a simple schema.
Stream structured responses
You can receive structured JSON responses in real-time as they are generated:
Best practices
- Start with simple schemas: Begin with basic object structures and add complexity only when needed.
- Keep schema fields focused and related: Schemas with unrelated fields may require multiple API calls to generate complete results. Group related analysis tasks together or make separate calls for different types of analyses.
- Keep field names descriptive: Use clear, consistent naming conventions for schema properties to make your code more maintainable.
- Mark essential fields as required: Use the
requiredproperty to specify fields that must be present. This ensures critical data is always included in responses. - Handle truncated responses: For open-ended analysis, always check the
finish_reasonfield in your code. When it equalslength, use themax_tokensparameter to request shorter content, and implement retry logic to avoid incomplete JSON. - Validate responses client-side: Parse and validate the JSON response in your application to catch any formatting issues before processing the data.
- Test schemas thoroughly: Validate your schema with sample data before deploying to production. Use different video types and content lengths to ensure reliability.
Troubleshooting
This section provides solutions to common issues you might encounter when working with structured JSON responses.
Prompt and response mismatch
Problem: The response contains information different from what you requested in the prompt (for example, you asked for a summary but received chapters).
Cause: The schema takes precedence over the prompt. The platform generates content that matches the schema structure, regardless of what the prompt requests.
Solution: Ensure your prompt and schema align to request the same type of information.
Incomplete or invalid JSON
Problem: The response contains truncated JSON or fails to parse.
Cause: The response reached the token limit before completing the JSON structure.
Solution: Check the finish_reason field. When it equals length, the response was truncated. Increase the max_tokens parameter (the maximum is 4096), simplify your schema by removing optional fields, or make your prompt more concise. For complex requests, break them into multiple API calls.
Invalid schema error
Problem: You receive a BadRequestError with code response_format_invalid when making a request.
Cause: The schema contains unsupported constraints (such as minLength or maxLength) or invalid references (such as $ref pointing to non-existent definitions).
Solution: Review the json_schema field in the API Reference section for a list of the supported constraints. Ensure all $ref values point to definitions in $defs. Remove unsupported validation keywords and use only the supported data types and constraints.
JSON schema requirements
Your schema must adhere to the JSON Schema Draft 2020-12 specification and must meet the requirements below:
- Supported data types:
array,boolean,integer,null,number,object, andstring. - Schema constraints: Use validation keywords like
patternfor strings,minimumandmaximumfor integers,requiredfor objects, andminItemsfor arrays (accepts only 0 or 1). - Schema composition: You can only use
anyOffor combining schemas. - References: Define subschemas in
$defsand reference them with valid$refURIs pointing to internal subschemas.
For complete schema specifications, see the json_schema field in the API Reference section.