Response Parsers API
This module provides classes for parsing and validating LLM responses into structured data formats, such as Pydantic models, JSON, or XML.
Base Class
maticlib.core.parsers.base.BaseResponseParser
Bases: ABC, Generic[T]
Base class for response parsers.
A response parser is responsible for: 1. Providing structural instructions to the LLM. 2. Parsing the LLM's text output into a structured object.
Source code in maticlib/core/parsers/base.py
get_structure_instructions
abstractmethod
Provides instructions to the LLM on how to structure its output.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Formatting instructions for the prompt. |
parse
abstractmethod
Parses the raw text output from an LLM.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The raw text response from the model. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
T |
T
|
The parsed structured object. |
Pydantic Parser
maticlib.core.parsers.pydantic.PydanticResponseParser
Bases: BaseResponseParser[T], Generic[T]
Parses LLM output into a specific Pydantic model.
Ensures that the response not only conforms to JSON but matches the expected schema and types defined by the Pydantic model.
Source code in maticlib/core/parsers/pydantic.py
get_structure_instructions
Generates instructions based on the Pydantic model schema.
Source code in maticlib/core/parsers/pydantic.py
parse
Parses text into a Pydantic model instance.
Source code in maticlib/core/parsers/pydantic.py
JSON Parser
maticlib.core.parsers.json.JSONResponseParser
Bases: BaseResponseParser[Dict[str, Any]]
Parses LLM output into a JSON/dictionary format.
Includes robust extraction logic to find JSON blocks even if the model returns conversational text surrounding the JSON.
Source code in maticlib/core/parsers/json.py
get_structure_instructions
Instructions for JSON output.
parse
Parses text into a dictionary. Handles markdown formatting.
Source code in maticlib/core/parsers/json.py
XML Parser
maticlib.core.parsers.xml.XMLResponseParser
Bases: BaseResponseParser[Dict[str, Any]]
Parses LLM output into a dictionary using XML tags as structure.
Source code in maticlib/core/parsers/xml.py
get_structure_instructions
Instructions for XML output.
parse
Extracts XML blocks and converts them into a flat dictionary.