Skip to content

Commit 252c69b

Browse files
authored
fix: stream reasoning_content in LM Studio provider (#1175)
* fix: stream reasoning_content in LM Studio provider The LM Studio handler only read `delta.content` and relied on TagMatcher to extract <think>/<thought> tags. Reasoning models served by LM Studio (Qwen3, DeepSeek-R1, QwQ) stream thinking in a dedicated `reasoning_content` field, which TagMatcher never sees, so the whole thought stream was dropped. The same model via the OpenAI Compatible provider worked because that handler already calls the shared `extractReasoningFromDelta` helper — LM Studio was the last provider missing it. Reasoning text is now also counted toward output tokens; thinking models previously reported ~0 output tokens for turns that were mostly reasoning. Fixes #1075 * test(lmstudio): assert reasoning_content wins over reasoning Add the both-fields-present case for the LM Studio stream handler, mirroring the existing test in openai.spec.ts. The handler calls extractReasoningFromDelta once per delta, so a delta carrying both reasoning_content and reasoning must yield exactly one reasoning chunk. The helper's own unit tests cover the precedence rule in isolation, but no provider-level test pinned it end-to-end here. Asserting on the filtered array rather than a single membership check fixes the chunk count too, so a regression that emits both fields is caught. Signed-off-by: daewoongoh <[email protected]> --------- Signed-off-by: daewoongoh <[email protected]>
1 parent b7f78f8 commit 252c69b

2 files changed

Lines changed: 97 additions & 1 deletion

File tree

‎src/api/providers/__tests__/lmstudio.spec.ts‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,89 @@ describe("LmStudioHandler", () => {
112112
expect(textChunks[0].text).toBe("Test response")
113113
})
114114

115+
it("streams reasoning chunks from delta.reasoning_content", async () => {
116+
// Regression: Qwen3 / DeepSeek-R1 style models served by LM Studio emit
117+
// thinking via reasoning_content, not <think> tags inside content.
118+
mockCreate.mockImplementationOnce(async () =>
119+
asyncStreamFrom([
120+
{ choices: [{ delta: { reasoning_content: "thinking..." }, index: 0 }] },
121+
{ choices: [{ delta: { content: "answer" }, index: 0 }] },
122+
{
123+
choices: [{ delta: {}, index: 0 }],
124+
usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 },
125+
},
126+
]),
127+
)
128+
129+
const chunks = await collectStream(handler.createMessage(systemPrompt, messages))
130+
131+
expect(chunks).toContainEqual({ type: "reasoning", text: "thinking..." })
132+
expect(chunks).toContainEqual({ type: "text", text: "answer" })
133+
})
134+
135+
it("falls back to delta.reasoning when reasoning_content is absent", async () => {
136+
mockCreate.mockImplementationOnce(async () =>
137+
asyncStreamFrom([
138+
{ choices: [{ delta: { reasoning: "router-style thought" }, index: 0 }] },
139+
{
140+
choices: [{ delta: {}, index: 0 }],
141+
usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 },
142+
},
143+
]),
144+
)
145+
146+
const chunks = await collectStream(handler.createMessage(systemPrompt, messages))
147+
148+
expect(chunks).toContainEqual({ type: "reasoning", text: "router-style thought" })
149+
})
150+
151+
it("prefers delta.reasoning_content over delta.reasoning when both are present", async () => {
152+
// When both reasoning_content and reasoning are set, only reasoning_content
153+
// should be emitted as a reasoning chunk (not both).
154+
mockCreate.mockImplementationOnce(async () =>
155+
asyncStreamFrom([
156+
{
157+
choices: [
158+
{
159+
delta: {
160+
reasoning_content: "primary thought",
161+
reasoning: "fallback thought",
162+
},
163+
index: 0,
164+
},
165+
],
166+
},
167+
{
168+
choices: [{ delta: {}, index: 0 }],
169+
usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 },
170+
},
171+
]),
172+
)
173+
174+
const chunks = await collectStream(handler.createMessage(systemPrompt, messages))
175+
176+
const reasoningChunks = chunks.filter((chunk) => chunk.type === "reasoning")
177+
178+
expect(reasoningChunks).toEqual([{ type: "reasoning", text: "primary thought" }])
179+
})
180+
181+
it("still parses <think> tags embedded in content", async () => {
182+
mockCreate.mockImplementationOnce(async () =>
183+
asyncStreamFrom([
184+
{ choices: [{ delta: { content: "<think>tagged thought</think>visible" }, index: 0 }] },
185+
{
186+
choices: [{ delta: {}, index: 0 }],
187+
usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 },
188+
},
189+
]),
190+
)
191+
192+
const chunks = await collectStream(handler.createMessage(systemPrompt, messages))
193+
194+
expect(chunks).toContainEqual({ type: "reasoning", text: "tagged thought" })
195+
expect(chunks).toContainEqual({ type: "text", text: "visible" })
196+
})
197+
115198
it("should handle API errors", async () => {
116199
mockCreate.mockRejectedValueOnce(new Error("API Error"))
117200

‎src/api/providers/lm-studio.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { BaseProvider } from "./base-provider"
2121
import type { SingleCompletionHandler, ApiHandlerCreateMessageMetadata, CompletePromptOptions } from "../index"
2222
import { getModelsFromCache } from "./fetchers/modelCache"
2323
import { handleOpenAIError } from "./utils/error-handler"
24+
import { extractReasoningFromDelta } from "./utils/extract-reasoning"
2425

2526
export class LmStudioHandler extends BaseProvider implements SingleCompletionHandler {
2627
protected options: ApiHandlerOptions
@@ -85,6 +86,7 @@ export class LmStudioHandler extends BaseProvider implements SingleCompletionHan
8586
}
8687

8788
let assistantText = ""
89+
let reasoningOutput = ""
8890

8991
try {
9092
const params: OpenAI.Chat.ChatCompletionCreateParamsStreaming & { draft_model?: string } = {
@@ -128,6 +130,15 @@ export class LmStudioHandler extends BaseProvider implements SingleCompletionHan
128130
}
129131
}
130132

133+
// Reasoning models served by LM Studio (Qwen3, DeepSeek-R1, QwQ, ...) stream
134+
// their thinking in a dedicated `reasoning_content`/`reasoning` delta field
135+
// rather than as <think> tags inside `content`, so TagMatcher never sees it.
136+
const reasoningText = extractReasoningFromDelta(delta)
137+
if (reasoningText) {
138+
reasoningOutput += reasoningText
139+
yield { type: "reasoning", text: reasoningText }
140+
}
141+
131142
// Handle tool calls in stream - emit partial chunks for NativeToolCallParser
132143
if (delta?.tool_calls) {
133144
for (const toolCall of delta.tool_calls) {
@@ -156,7 +167,9 @@ export class LmStudioHandler extends BaseProvider implements SingleCompletionHan
156167

157168
let outputTokens = 0
158169
try {
159-
outputTokens = await this.countTokens([{ type: "text", text: assistantText }])
170+
// Reasoning tokens are billed as output, so count them alongside the
171+
// visible text — otherwise thinking models under-report usage entirely.
172+
outputTokens = await this.countTokens([{ type: "text", text: reasoningOutput + assistantText }])
160173
} catch (err) {
161174
console.error("[LmStudio] Failed to count output tokens:", err)
162175
outputTokens = 0

0 commit comments

Comments
 (0)