Heuristic:ANative Lab EvoAgentX Parse Mode Auto Correction
| Knowledge Sources | |
|---|---|
| Domains | LLM, Structured_Output, Parsing |
| Last Updated | 2026-10-21 00:00 GMT |
Overview
Automatically corrects `parse_mode` to `"json"` when agent outputs contain `object` or `array` types, preventing structured output parsing failures.
Description
When creating a CustomizeAgent with `prompt_template`, the output schema is inspected for complex types. If any output parameter has type `object`, `array`, `dict`, or `list`, or has a `json_schema` defined, the `parse_mode` is force-corrected to `"json"`. This is because the `title` parse mode (the default) cannot reliably extract nested JSON objects or arrays from markdown-style headings. The correction happens at agent initialization and logs a warning so the user knows the mode was changed.
When using `prompt` (not `prompt_template`), the same check is applied: if outputs contain `object`/`array` types and `parse_mode` is not `"json"`, it is auto-corrected with a warning.
Usage
Use this heuristic when designing agents with structured outputs. You do not need to manually set `parse_mode="json"` for object/array outputs — the system auto-corrects it. However, explicitly setting `parse_mode="json"` suppresses the warning and makes the intent clear.
The Insight (Rule of Thumb)
- Action: Always use `parse_mode="json"` when outputs contain `object` or `array` types.
- Value: `"json"` (auto-corrected from `"title"` or other modes).
- Trade-off: JSON mode is stricter and may fail on malformed JSON, but it is the only reliable mode for nested structured data. The `title` mode works well for simple string/number outputs.
- Compatibility: Applies only when no custom `parse_func` is provided (a custom parse_func takes precedence).
Reasoning
The `title` parse mode extracts sections from markdown-style headings (e.g., `## field_name`). This works for flat string/number fields but cannot represent nested JSON objects or arrays. For example, an `array` of objects would need to be serialized as a JSON string within a markdown section, which is fragile and error-prone. By auto-correcting to `json` mode, the system ensures the LLM is instructed to produce valid JSON output that can be parsed deterministically.
The auto-correction only triggers when a custom `parse_func` is not supplied, because a custom parse function may handle complex types in its own way.
Code Evidence
Complex type detection from `evoagentx/agents/customize_agent.py:33`:
COMPLEX_PARAM_TYPES = {"object", "array", "dict", "list"}
Auto-correction logic from `evoagentx/agents/customize_agent.py:430-439`:
# Auto-correct parse_mode to "json" when outputs require it and no custom parse_func is provided.
if prompt_template is not None and CustomizeAgent._outputs_require_json_mode(valid_outputs, parse_func) and parse_mode != "json":
logger.warning(
f"parse_mode='{parse_mode}' is not compatible with the current outputs (object/array types or "
f"json_schema). Auto-correcting to parse_mode='json'. To suppress this warning, explicitly set "
f"parse_mode='json'."
)
parse_mode = "json"
JSON mode requirement check from `evoagentx/agents/customize_agent.py:302-306`:
def _outputs_require_json_mode(outputs, parse_func) -> bool:
if parse_func is not None:
return False
return any(p.json_schema or p.type in COMPLEX_PARAM_TYPES for p in outputs)
From-file auto-correction from `evoagentx/agents/customize_agent.py:743-751`:
if parse_mode != "json":
for output in outputs:
if output["type"] == "object" or output["type"] == "array":
agent_data["parse_mode"] = "json"
logger.warning(f"`parse_mode` is set to 'json' for '{agent_data['name']}' because it has outputs in format 'object' or 'array'")