Functions
parse_input()
Extract a JSON object from a text string, with optional dataclass validation.
Returns:
dict or dataclass instance
Raises:
ValueError- No valid JSON object found in promptTypeError- Schema is not a dataclassValueError- JSON doesn’t match dataclass (missing required fields)
parse_operation()
Extract an operation config using a discriminator field.
Returns: Dataclass instance for the matched operation
Raises:
ValueError - No valid operation config found
Example:
Extraction Strategy
Both functions search in this order:- Balanced-brace JSON objects - Hand-rolled scanner handles arbitrary nesting
- Code-fenced JSON blocks - Extracts from
```json ... ``` - Full prompt - Attempts to parse entire prompt as JSON
parse_operation(), only JSON objects containing an "operation" field are considered, and the discriminator value selects the schema.
JSON in Markdown
Input may look like:parse_input() and parse_operation() extract the JSON block correctly.
Dataclass Validation
When using a schema:- Only fields defined in the dataclass are extracted (unknown keys filtered)
- Missing required fields raise
ValueErrorwith clear message - Type hints are not enforced at runtime (Python limitation)
Error Messages
Clear errors for debugging:Real Example: Jira Agent
When to Use
Implementation Details
The balanced-brace scanner:- Handles arbitrary nesting depth (recursive objects/arrays)
- Tracks string boundaries and escape sequences
- Avoids miscounting braces inside string literals
- Returns all valid JSON objects found, tries each in order
See Also
How to Handle Structured Input
Task-oriented guide

