Skip to content

Commit dd0ff5b

Browse files
authored
test(litellm): add reasoning field handling coverage
Add tests verifying that reasoning_content and reasoning delta fields are correctly yielded as reasoning chunks, that reasoning_content takes precedence when both fields are present, and that empty/whitespace-only values are silently ignored.
1 parent d63d80c commit dd0ff5b

1 file changed

Lines changed: 118 additions & 0 deletions

File tree

‎src/api/providers/__tests__/lite-llm.spec.ts‎

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,124 @@ describe("LiteLLMHandler", () => {
719719
})
720720
})
721721

722+
describe("reasoning field handling", () => {
723+
it("should yield reasoning chunks from reasoning_content delta", async () => {
724+
const mockStream = {
725+
async *[Symbol.asyncIterator]() {
726+
yield {
727+
choices: [{ delta: { reasoning_content: "Let me think..." } }],
728+
usage: null,
729+
}
730+
yield {
731+
choices: [{ delta: { content: "The answer is 42." } }],
732+
usage: { prompt_tokens: 20, completion_tokens: 10 },
733+
}
734+
},
735+
}
736+
737+
mockCreate.mockReturnValue({
738+
withResponse: vi.fn().mockResolvedValue({ data: mockStream }),
739+
})
740+
741+
const generator = handler.createMessage("system", [{ role: "user", content: "What is the answer?" }])
742+
const results = []
743+
for await (const chunk of generator) {
744+
results.push(chunk)
745+
}
746+
747+
const reasoningChunk = results.find((c) => c.type === "reasoning")
748+
expect(reasoningChunk).toBeDefined()
749+
expect(reasoningChunk).toMatchObject({ type: "reasoning", text: "Let me think..." })
750+
751+
const textChunk = results.find((c) => c.type === "text")
752+
expect(textChunk).toMatchObject({ type: "text", text: "The answer is 42." })
753+
})
754+
755+
it("should yield reasoning chunks from reasoning delta field", async () => {
756+
const mockStream = {
757+
async *[Symbol.asyncIterator]() {
758+
yield {
759+
choices: [{ delta: { reasoning: "Analyzing the problem..." } }],
760+
usage: null,
761+
}
762+
yield {
763+
choices: [{ delta: { content: "Done." } }],
764+
usage: { prompt_tokens: 10, completion_tokens: 5 },
765+
}
766+
},
767+
}
768+
769+
mockCreate.mockReturnValue({
770+
withResponse: vi.fn().mockResolvedValue({ data: mockStream }),
771+
})
772+
773+
const generator = handler.createMessage("system", [{ role: "user", content: "Solve this." }])
774+
const results = []
775+
for await (const chunk of generator) {
776+
results.push(chunk)
777+
}
778+
779+
const reasoningChunk = results.find((c) => c.type === "reasoning")
780+
expect(reasoningChunk).toBeDefined()
781+
expect(reasoningChunk).toMatchObject({ type: "reasoning", text: "Analyzing the problem..." })
782+
})
783+
784+
it("should prefer reasoning_content over reasoning when both are present", async () => {
785+
const mockStream = {
786+
async *[Symbol.asyncIterator]() {
787+
yield {
788+
choices: [
789+
{ delta: { reasoning_content: "from_reasoning_content", reasoning: "from_reasoning" } },
790+
],
791+
usage: { prompt_tokens: 5, completion_tokens: 5 },
792+
}
793+
},
794+
}
795+
796+
mockCreate.mockReturnValue({
797+
withResponse: vi.fn().mockResolvedValue({ data: mockStream }),
798+
})
799+
800+
const generator = handler.createMessage("system", [{ role: "user", content: "Test." }])
801+
const results = []
802+
for await (const chunk of generator) {
803+
results.push(chunk)
804+
}
805+
806+
const reasoningChunks = results.filter((c) => c.type === "reasoning")
807+
expect(reasoningChunks).toHaveLength(1)
808+
expect(reasoningChunks[0]).toMatchObject({ type: "reasoning", text: "from_reasoning_content" })
809+
})
810+
811+
it("should not yield reasoning chunk for empty or whitespace-only reasoning", async () => {
812+
const mockStream = {
813+
async *[Symbol.asyncIterator]() {
814+
yield {
815+
choices: [{ delta: { reasoning_content: " " } }],
816+
usage: null,
817+
}
818+
yield {
819+
choices: [{ delta: { content: "Hello" } }],
820+
usage: { prompt_tokens: 5, completion_tokens: 5 },
821+
}
822+
},
823+
}
824+
825+
mockCreate.mockReturnValue({
826+
withResponse: vi.fn().mockResolvedValue({ data: mockStream }),
827+
})
828+
829+
const generator = handler.createMessage("system", [{ role: "user", content: "Hi" }])
830+
const results = []
831+
for await (const chunk of generator) {
832+
results.push(chunk)
833+
}
834+
835+
const reasoningChunks = results.filter((c) => c.type === "reasoning")
836+
expect(reasoningChunks).toHaveLength(0)
837+
})
838+
})
839+
722840
describe("tool ID normalization", () => {
723841
it("should truncate tool IDs longer than 64 characters", async () => {
724842
const optionsWithBedrock: ApiHandlerOptions = {

0 commit comments

Comments
 (0)