Basic usage
Generate structured JSON output using a schema:Schema types
Prism provides several schema types for defining structured output:String schema
Array schema
Object schema
Complete example
Here’s a complete example with nested schemas:use Prism\Prism\Schema\ObjectSchema;
use Prism\Prism\Schema\StringSchema;
use Prism\Prism\Schema\ArraySchema;
use Prism\Prism\Schema\IntegerSchema;
$schema = new ObjectSchema(
name: 'book_analysis',
description: 'Analysis of a book',
properties: [
new StringSchema('title', 'Book title'),
new StringSchema('author', 'Book author'),
new IntegerSchema('year', 'Publication year'),
new ArraySchema(
'themes',
'Main themes',
items: new StringSchema('theme', 'A theme')
),
new ArraySchema(
'characters',
'Main characters',
items: new ObjectSchema(
name: 'character',
description: 'Character details',
properties: [
new StringSchema('name', 'Character name'),
new StringSchema('role', 'Character role'),
]
)
),
]
);
use Prism\Prism\Prism;
use Prism\Vertex\Enums\Vertex;
$response = Prism::structured()
->using(Vertex::Gemini, 'gemini-2.5-flash')
->withSchema($schema)
->withPrompt('Analyze the book "1984" by George Orwell')
->asStructured();
$data = $response->structured;
// Access properties
echo $data['title']; // "1984"
echo $data['author']; // "George Orwell"
echo $data['year']; // 1949
// Access arrays
foreach ($data['themes'] as $theme) {
echo $theme;
}
// Access nested objects
foreach ($data['characters'] as $character) {
echo $character['name'] . ' - ' . $character['role'];
}
Provider support
All providers support structured output, but the implementation varies:- Gemini
- Anthropic
- OpenAI-compatible
Native structured outputGoogle Gemini models use native structured output via
response_mime_type: application/json and response_schema. The model is constrained to produce valid JSON matching your schema.Gemini provides the most reliable structured output with strict schema validation.
Customizing JSON instructions
For Anthropic and OpenAI schemas, you can customize the JSON instruction message:Schema implementation by provider
Each API schema handles structured output differently:| Schema | Implementation | Native Support |
|---|---|---|
| Gemini | response_mime_type: application/json + response_schema | Yes |
| Anthropic | Prompt-based instruction | No |
| OpenAI | response_format: { type: "json_object" } + instruction | Partial |
Despite implementation differences, all providers return valid JSON when using
asStructured().Error handling
Handle invalid schema or generation errors:Response object
TheasStructured() method returns a Prism\Prism\Structured\Response object:
Best practices
$response = Prism::structured()
->using(Vertex::Gemini, 'gemini-2.5-flash')
->withSchema($schema)
->withPrompt('Generate data')
->asStructured();
// Test with multiple providers
$providers = [
[Vertex::Gemini, 'gemini-2.5-flash'],
[Vertex::Anthropic, 'claude-3-5-sonnet@20241022'],
[Vertex::Mistral, 'mistral-small-2503'],
];
foreach ($providers as [$provider, $model]) {
$response = Prism::structured()
->using($provider, $model)
->withSchema($schema)
->withPrompt('Generate data')
->asStructured();
}
Next steps
Text generation
Generate text responses without schema constraints
Embeddings
Create text embeddings for semantic search
Multi-provider
Use multiple providers with different configurations