Skip to content

Commit 306b9a8

Browse files
committed
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
1 parent 19f306e commit 306b9a8

2 files changed

Lines changed: 67 additions & 1 deletion

File tree

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,59 @@ 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("still parses <think> tags embedded in content", async () => {
152+
mockCreate.mockImplementationOnce(async () =>
153+
asyncStreamFrom([
154+
{ choices: [{ delta: { content: "<think>tagged thought</think>visible" }, index: 0 }] },
155+
{
156+
choices: [{ delta: {}, index: 0 }],
157+
usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 },
158+
},
159+
]),
160+
)
161+
162+
const chunks = await collectStream(handler.createMessage(systemPrompt, messages))
163+
164+
expect(chunks).toContainEqual({ type: "reasoning", text: "tagged thought" })
165+
expect(chunks).toContainEqual({ type: "text", text: "visible" })
166+
})
167+
115168
it("should handle API errors", async () => {
116169
mockCreate.mockRejectedValueOnce(new Error("API Error"))
117170

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { BaseProvider } from "./base-provider"
1616
import type { SingleCompletionHandler, ApiHandlerCreateMessageMetadata, CompletePromptOptions } from "../index"
1717
import { getModelsFromCache } from "./fetchers/modelCache"
1818
import { handleOpenAIError } from "./utils/error-handler"
19+
import { extractReasoningFromDelta } from "./utils/extract-reasoning"
1920

2021
export class LmStudioHandler extends BaseProvider implements SingleCompletionHandler {
2122
protected options: ApiHandlerOptions
@@ -80,6 +81,7 @@ export class LmStudioHandler extends BaseProvider implements SingleCompletionHan
8081
}
8182

8283
let assistantText = ""
84+
let reasoningOutput = ""
8385

8486
try {
8587
const params: OpenAI.Chat.ChatCompletionCreateParamsStreaming & { draft_model?: string } = {
@@ -123,6 +125,15 @@ export class LmStudioHandler extends BaseProvider implements SingleCompletionHan
123125
}
124126
}
125127

128+
// Reasoning models served by LM Studio (Qwen3, DeepSeek-R1, QwQ, ...) stream
129+
// their thinking in a dedicated `reasoning_content`/`reasoning` delta field
130+
// rather than as <think> tags inside `content`, so TagMatcher never sees it.
131+
const reasoningText = extractReasoningFromDelta(delta)
132+
if (reasoningText) {
133+
reasoningOutput += reasoningText
134+
yield { type: "reasoning", text: reasoningText }
135+
}
136+
126137
// Handle tool calls in stream - emit partial chunks for NativeToolCallParser
127138
if (delta?.tool_calls) {
128139
for (const toolCall of delta.tool_calls) {
@@ -151,7 +162,9 @@ export class LmStudioHandler extends BaseProvider implements SingleCompletionHan
151162

152163
let outputTokens = 0
153164
try {
154-
outputTokens = await this.countTokens([{ type: "text", text: assistantText }])
165+
// Reasoning tokens are billed as output, so count them alongside the
166+
// visible text — otherwise thinking models under-report usage entirely.
167+
outputTokens = await this.countTokens([{ type: "text", text: reasoningOutput + assistantText }])
155168
} catch (err) {
156169
console.error("[LmStudio] Failed to count output tokens:", err)
157170
outputTokens = 0

0 commit comments

Comments
 (0)