Skip to content

symfonic.memory._internal.llm.response_parser

response_parser

Shared LLM response parsing utilities.

Extracts structured data from LLM text output. Used by both the extraction pipeline and the compaction engine to avoid duplicating the JSON-from-LLM parsing pattern.

extract_json_block

extract_json_block(content: str) -> dict[str, Any] | None

Extract the first JSON object from LLM text output.

Uses a simple brace-matching heuristic: finds the first { and the last } in content, then attempts json.loads on that substring.

Returns:

Type Description
dict[str, Any] | None

Parsed dict if a valid JSON object is found, otherwise None.

Source code in src/symfonic/memory/_internal/llm/response_parser.py
def extract_json_block(content: str) -> dict[str, Any] | None:
    """Extract the first JSON object from LLM text output.

    Uses a simple brace-matching heuristic: finds the first ``{`` and
    the last ``}`` in *content*, then attempts ``json.loads`` on that
    substring.

    Returns:
        Parsed dict if a valid JSON object is found, otherwise ``None``.
    """
    start = content.find("{")
    end = content.rfind("}") + 1
    if start < 0 or end <= start:
        return None
    try:
        return json.loads(content[start:end])
    except (json.JSONDecodeError, KeyError):
        return None

extract_llm_content

extract_llm_content(response: Any) -> str

Normalize LLM response to plain string content.

Source code in src/symfonic/memory/_internal/llm/response_parser.py
def extract_llm_content(response: Any) -> str:
    """Normalize LLM response to plain string content."""
    return response.content if hasattr(response, "content") else str(response)