Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions packages/llm/src/protocols/openai-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const OpenAIChatMessage = Schema.Union([
content: Schema.NullOr(Schema.String),
tool_calls: optionalArray(OpenAIChatAssistantToolCall),
reasoning_content: Schema.optional(Schema.String),
reasoning: Schema.optional(Schema.String),
}),
Schema.Struct({ role: Schema.Literal("tool"), tool_call_id: Schema.String, content: Schema.String }),
]).pipe(Schema.toTaggedUnion("role"))
Expand Down Expand Up @@ -145,6 +146,7 @@ type OpenAIChatToolCallDelta = Schema.Schema.Type<typeof OpenAIChatToolCallDelta
const OpenAIChatDelta = Schema.Struct({
content: optionalNull(Schema.String),
reasoning_content: optionalNull(Schema.String),
reasoning: optionalNull(Schema.String),
tool_calls: optionalNull(Schema.Array(OpenAIChatToolCallDelta)),
})

Expand Down Expand Up @@ -208,7 +210,13 @@ const lowerMedia = Effect.fn("OpenAIChat.lowerMedia")(function* (part: MediaPart
})

const openAICompatibleReasoningContent = (native: unknown) =>
isRecord(native) && typeof native.reasoning_content === "string" ? native.reasoning_content : undefined
isRecord(native)
? typeof native.reasoning_content === "string"
? native.reasoning_content
: typeof native.reasoning === "string"
? native.reasoning
: undefined
: undefined

const lowerUserMessage = Effect.fn("OpenAIChat.lowerUserMessage")(function* (message: OpenAIChatRequestMessage) {
const content: Array<Schema.Schema.Type<typeof OpenAIChatUserContent>> = []
Expand Down Expand Up @@ -416,8 +424,9 @@ const step = (state: ParserState, event: OpenAIChatEvent) =>

let lifecycle = state.lifecycle

if (delta?.reasoning_content)
lifecycle = Lifecycle.reasoningDelta(lifecycle, events, "reasoning-0", delta.reasoning_content)
const reasoningText = delta ? openAICompatibleReasoningContent(delta) : undefined
if (reasoningText)
lifecycle = Lifecycle.reasoningDelta(lifecycle, events, "reasoning-0", reasoningText)

if (delta?.content) {
lifecycle = Lifecycle.reasoningEnd(lifecycle, events, "reasoning-0")
Expand Down
26 changes: 26 additions & 0 deletions packages/llm/test/provider/openai-chat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,32 @@ describe("OpenAI Chat route", () => {
}),
)

it.effect("parses Ollama reasoning field deltas (reasoning alias for reasoning_content)", () =>
Effect.gen(function* () {
const body = sseEvents(
{ choices: [{ delta: { reasoning: "thinking" } }] },
{ choices: [{ delta: { content: "Hello" } }] },
{ choices: [{ delta: {}, finish_reason: "stop" }] },
)

const response = yield* LLMClient.generate(request).pipe(Effect.provide(fixedResponse(body)))

expect(response.reasoning).toBe("thinking")
expect(response.text).toBe("Hello")
expect(response.events).toMatchObject([
{ type: "step-start", index: 0 },
{ type: "reasoning-start", id: "reasoning-0" },
{ type: "reasoning-delta", id: "reasoning-0", text: "thinking" },
{ type: "reasoning-end", id: "reasoning-0" },
{ type: "text-start", id: "text-0" },
{ type: "text-delta", id: "text-0", text: "Hello" },
{ type: "text-end", id: "text-0" },
{ type: "step-finish", index: 0, reason: "stop" },
{ type: "finish", reason: "stop" },
])
}),
)

it.effect("assembles streamed tool call input", () =>
Effect.gen(function* () {
const body = sseEvents(
Expand Down
Loading