Spaces:
Sleeping
Sleeping
Download src/autodata_agent/core/json_utils.py from LostDecimal/adaa-agent-backend: direct link, hf CLI and curl.
- Browser
- Download file 1.34 kB
-
https://huggingface.co/spaces/LostDecimal/adaa-agent-backend/resolve/main/src/autodata_agent/core/json_utils.py
- Command line
-
hf download hf://spaces/LostDecimal/adaa-agent-backend/src/autodata_agent/core/json_utils.py
-
curl -L -o json_utils.py https://huggingface.co/spaces/LostDecimal/adaa-agent-backend/resolve/main/src/autodata_agent/core/json_utils.py
1.34 kB
| from __future__ import annotations | |
| import json | |
| import re | |
| from typing import Any | |
| from autodata_agent.core.errors import ValidationAppError | |
| def extract_json_object(text: str) -> dict[str, Any]: | |
| """Extract one JSON object from an LLM response.""" | |
| stripped = text.strip() | |
| if stripped.startswith("```"): | |
| stripped = re.sub(r"^```(?:json)?", "", stripped, flags=re.IGNORECASE).strip() | |
| stripped = re.sub(r"```$", "", stripped).strip() | |
| try: | |
| value = json.loads(stripped) | |
| except json.JSONDecodeError: | |
| start = stripped.find("{") | |
| if start == -1: | |
| raise ValidationAppError( | |
| "invalid_llm_json", | |
| "The model did not return a JSON object.", | |
| details={"response_preview": stripped[:500]}, | |
| ) from None | |
| try: | |
| value, _ = json.JSONDecoder().raw_decode(stripped[start:]) | |
| except json.JSONDecodeError as exc: | |
| raise ValidationAppError( | |
| "invalid_llm_json", | |
| "The model returned malformed JSON.", | |
| details={"response_preview": stripped[:500], "json_error": str(exc)}, | |
| ) from exc | |
| if not isinstance(value, dict): | |
| raise ValidationAppError("invalid_llm_json", "The model response JSON must be an object.") | |
| return value | |