Skip to main content

Functions

parse_input()

Extract a JSON object from a text string, with optional dataclass validation.
Parameters: Returns: dict or dataclass instance Raises:
  • ValueError - No valid JSON object found in prompt
  • TypeError - Schema is not a dataclass
  • ValueError - JSON doesn’t match dataclass (missing required fields)
Example:

parse_operation()

Extract an operation config using a discriminator field.
Parameters: Returns: Dataclass instance for the matched operation Raises: ValueError - No valid operation config found Example:

Extraction Strategy

Both functions search in this order:
  1. Balanced-brace JSON objects - Hand-rolled scanner handles arbitrary nesting
  2. Code-fenced JSON blocks - Extracts from ```json ... ```
  3. Full prompt - Attempts to parse entire prompt as JSON
For 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:
Both 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 ValueError with 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
This hand-rolled approach is necessary because regex cannot handle arbitrary nesting depth reliably.

See Also

How to Handle Structured Input

Task-oriented guide