LimitOutputLength
Requires the model’s output to fall within the given bounds.
Parameters
Section titled “Parameters”| Parameter | Type | Default | Description |
|---|---|---|---|
min_chars |
int | None |
None |
Minimum characters |
max_chars |
int | None |
None |
Maximum characters |
min_words |
int | None |
None |
Minimum words |
max_words |
int | None |
None |
Maximum words |
min_sentences |
int | None |
None |
Minimum sentences |
on_fail |
'retry' | 'raise' | 'warn' |
'retry' |
See below |
Set at least one bound, or construction raises ValueError.
Examples
Section titled “Examples”from pydantic_ai import Agentfrom pydantic_ai_guardrails import LimitOutputLength
agent = Agent( 'openai:gpt-4o', capabilities=[LimitOutputLength(min_words=50, max_words=200, min_sentences=3)],)
result = agent.run_sync('Summarise the plot of Hamlet.')print(result.output)Failure behaviour
Section titled “Failure behaviour”on_fail='retry'(default): raisesModelRetry. The model sees the reason and tries again, up to the agent’s output retry budget (Agent(retries={'output': N})).on_fail='raise': raisesOutputBlockedwith.guardrailand.reason.on_fail='warn': logs to thepydantic_ai_guardrailslogger and returns the output unchanged.