Skip to content

Conversation

@christian-bromann
Copy link
Member

Fixes an issue where responseFormat with providerStrategy returns undefined when used with thinking/reasoning models (e.g., Claude with thinking enabled, OpenAI o1/o3).

fixes #9421

Problem

When using thinking models, the AI message content is returned as an array of content blocks (containing thinking blocks and text blocks), rather than a simple string:

// Thinking model response structure
content: [
  { type: "thinking", thinking: "..." },
  { type: "text", text: '{"shapeName": "circle", ...}' }  // ← Structured output is here
]

The ProviderStrategy.parse() method only handled string content, causing it to return undefined for all thinking model responses:

// BEFORE (broken)
if (typeof response.content !== "string" || response.content === "") {
  return; // Returns undefined for array content!
}

Solution

Updated ProviderStrategy.parse() to handle both string content and array content by extracting text from text blocks:

// AFTER (fixed)
let textContent: string | undefined;

if (typeof response.content === "string") {
  textContent = response.content;
} else if (Array.isArray(response.content)) {
  // For thinking models, extract text from text blocks
  for (const block of response.content) {
    if (block.type === "text" && typeof block.text === "string") {
      textContent = block.text;
      break;
    }
  }
}

@changeset-bot
Copy link

changeset-bot bot commented Dec 11, 2025

🦋 Changeset detected

Latest commit: e270990

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
langchain Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@christian-bromann christian-bromann merged commit ade8b8a into main Dec 12, 2025
24 of 25 checks passed
@christian-bromann christian-bromann deleted the cb/thinking-structured-output branch December 12, 2025 00:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

responseFormat issue in v1

2 participants