fix(agent/json_utils): Decode as JSON rather than Python objects

* Replace `ast.literal_eval` with `json.loads` in `extract_dict_from_response`

This fixes a bug where boolean values could not be decoded because of their required capitalization in Python.
pull/6774/head
Reinier van der Leer 2024-01-31 14:15:02 +01:00
parent 83a0b03523
commit 956cdc77fa
No known key found for this signature in database
GPG Key ID: CDC1180FDAE06193
1 changed files with 2 additions and 4 deletions

View File

@ -1,5 +1,5 @@
"""Utilities for the json_fixes package."""
import ast
import json
import logging
import re
from typing import Any
@ -22,9 +22,7 @@ def extract_dict_from_response(response_content: str) -> dict[str, Any]:
if match:
response_content = match.group()
# Response content comes from OpenAI as a Python `str(content_dict)`.
# `literal_eval` does the reverse of `str(dict)`.
result = ast.literal_eval(response_content)
result = json.loads(response_content)
if not isinstance(result, dict):
raise ValueError(
f"Response '''{response_content}''' evaluated to "