# Structured Output

**Package:** Anthropic Laravel | **Version:** 1 | **URL:** https://mozex.dev/docs/anthropic-laravel/v1/usage/structured-output

---

Structured output makes Claude answer in JSON that matches a schema. In Laravel, `structured()` does the whole round trip: it asks for the format, checks the reply, and returns it.

```php
use Anthropic\Laravel\Facades\Anthropic;

class Contact
{
    public function __construct(
        public string $name,
        public string $email,
        public string $plan_interest,
        public bool $demo_requested,
    ) {}
}

$contact = Anthropic::messages()->structured([
    'model' => 'claude-sonnet-5-5',
    'max_tokens' => 1024,
    'messages' => [
        ['role' => 'user', 'content' => "Extract the key information from this email: {$email->body}"],
    ],
], Contact::class);

$contact->email; // 'john@example.com'
```

## Schemas from what you already have

`structured()` and `Anthropic\Laravel\Schema\Schema::format()` take any of these:

| Schema | Example |
|--------|---------|
| A class | `Contact::class`, built from its constructor ([what a class can hold](https://mozex.dev/docs/anthropic-php/v1/usage/structured-output#what-a-class-can-hold)) |
| An `Illuminate\JsonSchema` type | `JsonSchema::object(['name' => JsonSchema::string()->max(50)->required()])` |
| A laravel/ai-style array of property types | `['name' => JsonSchema::string()->required()]` |
| Validation rules | `['name' => 'required\|string\|max:255', 'email' => 'required\|email', 'tags.*' => 'string']` |
| A JSON schema array | An object schema, or one with `properties`, `anyOf`, `allOf`, `oneOf` or a `$ref` at its root, sent as it is |

A class, a type and a property array come back as the class or the checked array. With rules, the request carries the JSON schema the rules can say (types, `required`, `nullable`, `in`, `Rule::in()`, `Rule::enum()`, `min`/`max`, `decimal` as a string, formats like `email` and `url`, a simple `regex`, `array:street,city`, and `address.city` or `tags.*` nesting), and the reply is checked against every rule by Laravel's validator. That includes rules no schema can say, such as `exists` or `ends_with`, and you get back `validated()`. An `in` value Claude wrote in another case comes back in the case your rule gives it.

Constraints Claude doesn't take, like a string's `max`, move into the property's description before the request goes out, and the check afterwards still enforces them. A JSON schema array of your own is the exception: it goes as it is, so run it through `Anthropic\Schema\Schema::sanitize()` first if it has any.

## When the reply doesn't fit

A reply that fails the check gets one more try. The reply and the list of what's wrong go back to Claude, so expect a second billed call. `retries:` sets how many more tries it gets, and `0` turns the retry off. A reply that still fails, a refusal, a reply cut off at `max_tokens` and one without JSON throw `Anthropic\Exceptions\StructuredOutputException`. Only a failed check is retried, and not when the reply also calls one of your tools.

```php
use Anthropic\Exceptions\StructuredOutputException;
use Illuminate\Support\Facades\Log;

try {
    $contact = Anthropic::messages()->structured($request, Contact::class);
} catch (StructuredOutputException $e) {
    Log::warning('Contact extraction failed', ['reason' => $e->reason, 'errors' => $e->errors]);
}
```

## Doing it yourself

For a request you build yourself, put the format in `output_config` and parse the response:

```php
use Anthropic\Laravel\Schema\Schema;

$response = Anthropic::messages()->create([...$request, 'output_config' => ['format' => Schema::format(Contact::class)]]);

$contact = $response->parse(Contact::class);
```

A stream has `parse()` too, and `countTokens()` takes the same `output_config`.

---

For the class attributes, the sanitizer and `parse()`'s errors, see the [Structured Output page in the PHP docs](https://mozex.dev/docs/anthropic-php/v1/usage/structured-output) or Anthropic's [structured outputs guide](https://platform.claude.com/docs/en/build-with-claude/structured-outputs).

---

## Table of Contents

- [Introduction](https://mozex.dev/docs/anthropic-laravel/v1)
- [AI Integration](https://mozex.dev/docs/anthropic-laravel/v1/ai-integration)
- [Support Us](https://mozex.dev/docs/anthropic-laravel/v1/support-us)
- [Requirements](https://mozex.dev/docs/anthropic-laravel/v1/requirements)
- [Changelog](https://mozex.dev/docs/anthropic-laravel/v1/changelog)
- [Contributing](https://mozex.dev/docs/anthropic-laravel/v1/contributing)
- [Questions & Issues](https://mozex.dev/docs/anthropic-laravel/v1/questions-and-issues)
- [About Mozex](https://mozex.dev/docs/anthropic-laravel/v1/about)

### Usage

- [Messages](https://mozex.dev/docs/anthropic-laravel/v1/usage/messages)
- [Streaming](https://mozex.dev/docs/anthropic-laravel/v1/usage/streaming)
- [Structured Output](https://mozex.dev/docs/anthropic-laravel/v1/usage/structured-output)
- [Tool Use](https://mozex.dev/docs/anthropic-laravel/v1/usage/tool-use)
- [Thinking](https://mozex.dev/docs/anthropic-laravel/v1/usage/thinking)
- [Server Tools](https://mozex.dev/docs/anthropic-laravel/v1/usage/server-tools)
- [Citations](https://mozex.dev/docs/anthropic-laravel/v1/usage/citations)
- [Token Counting](https://mozex.dev/docs/anthropic-laravel/v1/usage/token-counting)
- [Models](https://mozex.dev/docs/anthropic-laravel/v1/usage/models)
- [Batches](https://mozex.dev/docs/anthropic-laravel/v1/usage/batches)
- [Completions](https://mozex.dev/docs/anthropic-laravel/v1/usage/completions)
- [Files](https://mozex.dev/docs/anthropic-laravel/v1/usage/files)
- [Raw Requests](https://mozex.dev/docs/anthropic-laravel/v1/usage/raw-requests)
- [Usage Ledger](https://mozex.dev/docs/anthropic-laravel/v1/usage/usage-ledger)
- [Token Budgets](https://mozex.dev/docs/anthropic-laravel/v1/usage/token-budgets)

### Reference

- [Configuration](https://mozex.dev/docs/anthropic-laravel/v1/reference/configuration)
- [Error Handling](https://mozex.dev/docs/anthropic-laravel/v1/reference/error-handling)
- [Meta Information](https://mozex.dev/docs/anthropic-laravel/v1/reference/meta-information)
- [Testing](https://mozex.dev/docs/anthropic-laravel/v1/reference/testing)
- [Using alongside laravel/ai](https://mozex.dev/docs/anthropic-laravel/v1/reference/laravel-ai)
- [Diagnostics](https://mozex.dev/docs/anthropic-laravel/v1/reference/diagnostics)