> ## Documentation Index
> Fetch the complete documentation index at: https://splinter.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Schema Validation

> Validate agent outputs against schemas

SchemaValidator ensures agent outputs match expected formats. Catches errors early, before they propagate.

## Basic Usage

```python theme={null}
from splinter.coordination import SchemaValidator

validator = SchemaValidator()

schema = {
    "type": "object",
    "properties": {
        "findings": {"type": "array"},
        "confidence": {"type": "number", "minimum": 0, "maximum": 1},
    },
    "required": ["findings"],
}

# Validate output
output = {"findings": ["a", "b"], "confidence": 0.9}
validator.validate(output, schema)  # ✓ OK

# Invalid output
bad_output = {"confidence": 1.5}  # Missing findings, confidence > 1
validator.validate(bad_output, schema)  # ✗ Raises SchemaValidationError
```

## JSON Schema

We use standard JSON Schema:

```python theme={null}
schema = {
    "type": "object",
    "properties": {
        "title": {"type": "string", "minLength": 1},
        "tags": {
            "type": "array",
            "items": {"type": "string"},
            "minItems": 1,
        },
        "metadata": {
            "type": "object",
            "properties": {
                "author": {"type": "string"},
                "date": {"type": "string", "format": "date"},
            },
        },
    },
    "required": ["title", "tags"],
}
```

## Registering Agent Schemas

```python theme={null}
validator = SchemaValidator()

# Register schema for agent output
validator.register_agent_schema("researcher", research_schema)
validator.register_agent_schema("writer", article_schema)

# Validate agent output automatically
validator.validate_agent_output("researcher", output)
```

## Schema from Example

Generate schema from example data:

```python theme={null}
from splinter.coordination import create_schema_from_example

example = {
    "findings": ["trend1", "trend2"],
    "sources": ["url1", "url2"],
    "confidence": 0.85,
}

schema = create_schema_from_example(example)
# Automatically infers types and structure
```

## Handling Validation Errors

```python theme={null}
from splinter.exceptions import SchemaValidationError

try:
    validator.validate(output, schema)
except SchemaValidationError as e:
    print(f"Validation failed: {e.message}")
    print(f"Path: {e.path}")
    print(f"Value: {e.value}")
    
    # Get all errors
    for error in e.errors:
        print(f"  - {error.path}: {error.message}")
```

## Partial Validation

Sometimes you want to validate incrementally:

```python theme={null}
# Validate specific fields only
validator.validate_fields(output, schema, fields=["title", "tags"])

# Validate structure but not values
validator.validate_structure(output, schema)
```

## Best Practices

<AccordionGroup>
  <Accordion title="Define schemas upfront">
    Before building agents, define what they should output.
  </Accordion>

  <Accordion title="Use required fields">
    Don't assume optional fields will be present.
  </Accordion>

  <Accordion title="Validate early">
    Check outputs immediately after agent runs, not later.
  </Accordion>

  <Accordion title="Include error context">
    When validation fails, include what was expected vs received.
  </Accordion>
</AccordionGroup>
