File size: 1,102 Bytes
b0d0019
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
Post-processing utilities for raw agent outputs.
"""

import json
import logging
from pathlib import Path
from typing import Any, Dict

from app.core.schemas import AgentResponse

logger = logging.getLogger(__name__)


def save_agent_response(response: AgentResponse, output_path: str) -> None:
    """
    Persist an AgentResponse to a JSON file.

    Args:
        response: Parsed AgentResponse Pydantic model.
        output_path: Destination file path.
    """
    path = Path(output_path)
    path.parent.mkdir(parents=True, exist_ok=True)

    response_dict = response.model_dump(mode="json")

    with open(path, "w", encoding="utf-8") as f:
        json.dump(response_dict, f, indent=4, ensure_ascii=False)

    logger.info("Saved structured agent response to %s", path.resolve())


def agent_response_to_dict(response: AgentResponse) -> Dict[str, Any]:
    """
    Convert an AgentResponse Pydantic model to a plain dict for JSON serialisation.

    Args:
        response: Parsed AgentResponse.

    Returns:
        Plain dictionary.
    """
    return response.model_dump(mode="json")