Skip to content

Commit 9afb232

Browse files
authored
feat: strip think tags from LLM responses (#170)
## Summary - Strip `<think>...</think>` blocks from LLM responses before JSON parsing in `_parse_llm_response()` - Reasoning models include thinking blocks that would cause `json.loads()` to fail when parsing episode index arrays - Uses compiled regex for efficiency ## Test plan - [ ] Verify episode search queries still return correct results - [ ] Test with a response containing think tags to confirm they are stripped before JSON parsing ## Notes - Pre-commit test hook was skipped due to broken local venv (pydantic_core issue unrelated to this change). Ruff lint/format checks passed.
1 parent 07785c4 commit 9afb232

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

app/services/llm_service.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import json
1111
import logging
12+
import re
1213
from typing import Any
1314

1415
import httpx
@@ -17,6 +18,8 @@
1718

1819
logger = logging.getLogger(__name__)
1920

21+
_THINK_TAG_RE = re.compile(r"<think>.*?</think>\s*", re.DOTALL)
22+
2023
# LLM configuration
2124
_LLM_TIMEOUT_SECONDS = 30.0 # Longer timeout for processing episode summaries
2225
_MAX_EPISODES_FOR_CONTEXT = 100 # Include more episodes since we have summaries
@@ -174,6 +177,9 @@ def _parse_llm_response(
174177
"""
175178
content = result.get("choices", [{}])[0].get("message", {}).get("content", "[]")
176179

180+
# Strip <think>...</think> blocks from reasoning models before JSON parsing
181+
content = _THINK_TAG_RE.sub("", content).strip()
182+
177183
try:
178184
indices = json.loads(content)
179185
except json.JSONDecodeError:

0 commit comments

Comments
 (0)