feat: extract JSON from thinking preamble in ThinkingAwareOpenAILike#238
Merged
neoneye merged 1 commit intoPlanExeOrg:mainfrom Mar 10, 2026
Merged
Conversation
When LM Studio's Qwen 3.5-35B runs with thinking enabled, reasoning_content
contains the full thinking preamble concatenated with the final JSON answer.
Passing this raw text to Pydantic's model_validate_json fails because it sees
thinking prose + JSON rather than just JSON.
Add _extract_json_from_thinking() with a 3-strategy approach:
1. Direct parse — fast path for responses where reasoning_content is already JSON
2. </think> tag — extract content after the tag (some models emit this marker)
3. Right-to-left scan — find the rightmost '{' that yields a valid JSON parse
The extracted JSON (or original text on failure) replaces reasoning_content
in the ChatResponse, so downstream Pydantic validation works correctly.
Tested on Qwen 3.5-35B (Q4_K_M, LM Studio 0.3.x) with thinking always-on:
fixed CreateWBSLevel3Task failures in real pipeline runs where the task was
receiving 12,000+ char reasoning_content with JSON buried at the end.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When LM Studio's Qwen 3.5-35B runs with thinking enabled,
reasoning_contentcontains the full thinking preamble concatenated with the final JSON answer. PR #231 correctly falls back toreasoning_contentwhencontentis empty — but then passes the raw mixed text to Pydantic'smodel_validate_json, which fails because it sees 12,000+ chars of thinking prose before the JSON object.This caused
CreateWBSLevel3Taskto fail mid-pipeline on real runs with Qwen 3.5-35B thinking mode.Solution
Add
_extract_json_from_thinking()with a 3-strategy approach:reasoning_contentis already valid JSON</think>tag — extract content after the tag (DeepSeek and some other models emit this marker to cleanly separate thinking from output){that yields a valid JSON parse (handles Qwen 3.5-35B which does not emit</think>)If all strategies fail, the original text is returned unchanged (existing behavior — caller handles the failure).
Testing
Tested on Qwen 3.5-35B (Q4_K_M, LM Studio 0.3.x) with thinking always-on. The function correctly extracts JSON from
reasoning_contentstrings of 12,000–19,000 chars. FixedCreateWBSLevel3Taskfailures on Batman RICO v10 pipeline run (Hartford CT variant).Compatibility
reasoning_contentis not present → code path unchanged</think>: strategy 2 handles them efficiently</think>(Qwen 3.5-35B via LM Studio): strategy 3 handles themreasoning_contentis already valid JSON (short non-thinking responses): strategy 1 returns immediately