Overview
Concrete tool for parsing structured data from LLM-generated text using multiple parse modes (str, json, xml, title, custom).
Description
The LLMOutputParser class is a Pydantic-based parser that extracts structured data from raw LLM output strings. It supports five parse modes via the `PARSER_VALID_MODE` set: `"str"` (plain text), `"json"` (JSON parsing with schema validation), `"xml"` (XML tag extraction), `"title"` (section title parsing with configurable format), and `"custom"` (user-supplied parse function). The `get_content_data` method dispatches to the appropriate parser based on the active mode.
Usage
- CustomizeAgent: The `parse_mode` parameter on `CustomizeAgent` selects which mode the LLMOutputParser uses to parse agent output.
- Structured output: Use `parse_mode="json"` when outputs include object/array types or `json_schema` fields.
- Multi-agent debate: Debater agents use `parse_mode="xml"` to extract thought/argument/answer fields.
Code Reference
Source Location
- Repository: EvoAgentX
- File: evoagentx/models/base_model.py (L29-589)
Signature
PARSER_VALID_MODE = ["str", "json", "xml", "title", "custom"]
class LLMOutputParser(BaseModel):
model_config = ConfigDict(arbitrary_types_allowed=True)
def get_structured_data(self) -> dict
@classmethod
def parse(cls, content: str, parse_mode: str = "str", parser=None, parse_func=None, title_format="## {title}") -> 'LLMOutputParser'
@classmethod
def get_content_data(cls, content: str, parse_mode: str = "str", parser=None, parse_func=None, title_format="## {title}") -> dict
Import
from evoagentx.models.base_model import LLMOutputParser, PARSER_VALID_MODE
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| content |
str |
Yes |
Raw LLM output text
|
| parse_mode |
str |
No |
One of PARSER_VALID_MODE (default: "str")
|
| parser |
Type[LLMOutputParser] |
No |
Pydantic model class for structured validation
|
| parse_func |
Callable |
Required for "custom" |
Function accepting `content: str`, returning `dict`
|
| title_format |
str |
No |
Title pattern with `{title}` placeholder (default: "## {title}")
|
Outputs
| Name |
Type |
Description
|
| get_structured_data returns |
dict |
Parsed fields as a dictionary
|
| parse returns |
LLMOutputParser |
Parser instance with populated fields
|
Usage Examples
JSON Parsing
from evoagentx.models.base_model import LLMOutputParser
class MyOutput(LLMOutputParser):
name: str = ""
age: str = ""
content = '{"name": "Alice", "age": "30"}'
parsed = MyOutput.parse(content, parse_mode="json")
print(parsed.get_structured_data()) # {"name": "Alice", "age": "30"}
Title Parsing
from evoagentx.models.base_model import LLMOutputParser
class ReportOutput(LLMOutputParser):
summary: str = ""
details: str = ""
content = "## summary\nThe project is on track.\n## details\nAll milestones met."
parsed = ReportOutput.parse(content, parse_mode="title", title_format="## {title}")
print(parsed.get_structured_data())
# {"summary": "The project is on track.", "details": "All milestones met."}
Related Pages
Implements Principle
Requires Environment
Uses Heuristic
Page Connections
Double-click a node to navigate. Hold to expand connections.