Skip to content

Reasoning

For reasoning models, the SDK also outputs ReasoningPart:

types.ts
interface ReasoningPart {
type: "reasoning";
/**
* The reasoning text content
*/
text: string;
/**
* The reasoning internal signature
*/
signature?: string;
/**
* The ID of the reasoning part, if applicable
*/
id?: string;
}

This may enabled by default for supported models, but can be configured using reasoning input:

types.ts
interface ReasoningOptions {
/**
* Whether to enable reasoning output.
*/
enabled: boolean;
/**
* Specify the budget tokens for reasoning generation.
*/
budget_tokens?: number;
}

Once enabled, the model will output ReasoningParts in addition to other parts.

generate-reasoning

generate-reasoning.ts
import type { Part } from "@hoangvvo/llm-sdk";
import { getModel } from "./get-model.ts";
const model = getModel("openai", "o1");
const response = await model.generate({
messages: [
{
role: "user",
content: [
{
type: "text",
text: `A car starts from rest and accelerates at a constant rate of 4 m/s^2 for 10 seconds.
1. What is the final velocity of the car after 10 seconds?
2. How far does the car travel in those 10 seconds?`,
},
],
},
],
reasoning: {
enabled: true,
},
});
const { reasoningParts, otherParts } = response.content.reduce(
(acc, part) => {
if (part.type === "reasoning") {
acc.reasoningParts.push(part);
} else {
acc.otherParts.push(part);
}
return acc;
},
{ reasoningParts: [] as Part[], otherParts: [] as Part[] },
);
console.log("Reasoning");
console.dir(reasoningParts, { depth: null });
console.log("\nAnswer");
console.dir(otherParts, { depth: null });

Reasoning parts are also emitted in streaming mode in the form of ReasoningPartDelta.

stream-reasoning

types.ts
interface ReasoningPartDelta {
type: "reasoning";
/**
* The reasoning text content
*/
text: string;
/**
* The reasoning internal signature
*/
signature?: string;
/**
* The ID of the reasoning part, if applicable
*/
id?: string;
}

Accessing reasoning parts in streaming mode like other parts:

stream-reasoning.ts
import { StreamAccumulator } from "@hoangvvo/llm-sdk";
import { getModel } from "./get-model.ts";
const model = getModel("openai", "o1");
const stream = model.stream({
messages: [
{
role: "user",
content: [
{
type: "text",
text: `A car starts from rest and accelerates at a constant rate of 4 m/s^2 for 10 seconds.
1. What is the final velocity of the car after 10 seconds?
2. How far does the car travel in those 10 seconds?`,
},
],
},
],
reasoning: {
enabled: true,
},
});
const accumulator = new StreamAccumulator();
let current = await stream.next();
while (!current.done) {
if (current.value.delta?.part.type === "reasoning") {
console.log("Reasoning:");
console.dir(current.value.delta.part, { depth: null });
} else if (current.value.delta) {
console.log("Answer:");
console.dir(current.value.delta.part, { depth: null });
}
accumulator.addPartial(current.value);
current = await stream.next();
}
const finalResponse = accumulator.computeResponse();
console.dir(finalResponse, { depth: null });