Skip to content

AI-powered virtual executive team — a single coherent executive persona backed by 8 specialist agents (FastAPI + Next.js).

fix(chat): find the JSON object around prose in fast-model answers (#488)

Rufusdf9d7a003b04
diff --git a/packages/core/openexecutive/api/routes/chat.py b/packages/core/openexecutive/api/routes/chat.py
index 2e65890b..6ec12f6d 100644
--- a/packages/core/openexecutive/api/routes/chat.py
+++ b/packages/core/openexecutive/api/routes/chat.py
@@ -2111,17 +2111,27 @@ async def _fast_json_call(
log_model_usage(response, model=model, actor=log_tag)
text_blocks = [b for b in response.content if getattr(b, "type", "") == "text"]
raw = text_blocks[0].text.strip() if text_blocks else ""
- if raw.startswith("```"):
- # Strip ```json fences if the model added them anyway. Use
- # removeprefix (not lstrip) — lstrip("json") would also strip
- # legitimate leading j/s/o/n characters from the JSON body.
- parts = raw.split("```")
- if len(parts) >= 2:
- raw = parts[1].removeprefix("json").strip()
- data = json.loads(raw)
except Exception:
logger.exception("%s: LLM call failed", log_tag)
return None
+ data = _json_object(raw)
+ if data is None:
+ # The model's formatting slip, not a fault: the caller falls back.
+ logger.warning("%s: no JSON object in the answer (%d chars)", log_tag, len(raw))
+ return data
+
+
+def _json_object(raw: str) -> dict[str, Any] | None:
+ """The JSON object in a model's answer, or None. Tolerates what the
+ model adds around it despite the prompt: ```json fences, a sentence
+ before or after."""
+ start, end = raw.find("{"), raw.rfind("}")
+ if start < 0 or end < start:
+ return None
+ try:
+ data = json.loads(raw[start : end + 1])
+ except ValueError:
+ return None
return data if isinstance(data, dict) else None
diff --git a/packages/core/tests/unit/test_session_followup.py b/packages/core/tests/unit/test_session_followup.py
index 2eb6df6f..d2bafc6b 100644
--- a/packages/core/tests/unit/test_session_followup.py
+++ b/packages/core/tests/unit/test_session_followup.py
@@ -11,6 +11,7 @@ Covers:
from __future__ import annotations
from collections.abc import Iterator
+from types import SimpleNamespace
from typing import Any
import pytest
@@ -203,7 +204,38 @@ async def test_generator_parses_fenced_json(monkeypatch: pytest.MonkeyPatch) ->
@pytest.mark.parametrize(
"raw",
[
+ 'Here is the next message:\n{"suggestion": "Draft the Pro pricing email."}',
+ '{"suggestion": "Draft the Pro pricing email."}\nThat builds on the last reply.',
+ ],
+ ids=["sentence before", "sentence after"],
+)
+async def test_generator_finds_the_object_around_prose(monkeypatch: pytest.MonkeyPatch, raw: str) -> None:
+ _patch_provider(monkeypatch, raw)
+ assert await chat_route._generate_followup_via_llm("t") == "Draft the Pro pricing email."
+
+
+async def test_a_formatting_slip_is_a_warning_not_an_error(monkeypatch: pytest.MonkeyPatch) -> None:
+ # A recording logger, not caplog: other modules change the
+ # "openexecutive" logger's propagation in a full run.
+ calls: list[str] = []
+ fake = SimpleNamespace(
+ warning=lambda *a, **k: calls.append("warning"),
+ exception=lambda *a, **k: calls.append("exception"),
+ )
+ monkeypatch.setattr(chat_route, "logger", fake)
+ _patch_provider(monkeypatch, "Sure, happy to help.")
+ assert await chat_route._generate_followup_via_llm("t") is None
+ assert calls == ["warning"]
+
+
+ "raw",
+ [
+ "",
"not json",
+ "{not json}",
'["a list"]',
'{"suggestion": ""}',
'{"suggestion": 42}',