Skip to content

fix(cookbook): use structured output in JS LangChain prompt example#3198

Merged
hassiebp merged 4 commits into
mainfrom
feature/lf-2221-update-js-langchain-prompt-management-cookbook-output-parser
Jul 3, 2026
Merged

fix(cookbook): use structured output in JS LangChain prompt example#3198
hassiebp merged 4 commits into
mainfrom
feature/lf-2221-update-js-langchain-prompt-management-cookbook-output-parser

Conversation

@milanagm

@milanagm milanagm commented Jun 29, 2026

Copy link
Copy Markdown
Collaborator

What changed

Updates the JS LangChain prompt management cookbook to use the current LangChain structured output API.

The previous example used JsonOutputFunctionsParser from langchain/output_parsers together with model.bind(...).pipe(parser). With the current LangChain JS packages, that path is stale: model.bind is no longer available on ChatOpenAI, while withStructuredOutput(...) is the supported path for function-calling based structured output.

Why

This keeps the cookbook runnable with current LangChain JS versions and avoids a stale output parser import.

Related

Fixes Linear: LF-2221

Greptile Summary

Replaces a stale LangChain JS structured-output pattern (JsonOutputFunctionsParser + model.bind().pipe(parser)) with the current model.withStructuredOutput() API in both the MDX cookbook and its paired Jupyter notebook. The section heading is also updated to match the new approach.

  • Removes the JsonOutputFunctionsParser import and its instantiation, eliminating a dependency on the now-unsupported langchain/output_parsers path.
  • Introduces model.withStructuredOutput(extractionFunctionSchema, { method: \"functionCalling\" }), which is the recommended API for function-calling-based structured output in current @langchain/openai versions — the \"functionCalling\" method value is confirmed by the official LangChain JS reference docs.

Confidence Score: 5/5

Safe to merge — both changed files are updated consistently and the new API call is correct per official LangChain JS docs.

The migration replaces two removed APIs (model.bind and JsonOutputFunctionsParser) with withStructuredOutput, which is the current recommended path. The method: "functionCalling" camelCase value is confirmed correct by the LangChain JS reference. Both the MDX file and the paired notebook are updated in sync, so no version drift between the two formats.

No files require special attention.

Sequence Diagram

%%{init: {'theme': 'neutral'}}%%
sequenceDiagram
    participant User as User Code
    participant Langfuse
    participant Model as ChatOpenAI
    participant OpenAI as OpenAI API

    Note over User,OpenAI: Before (stale)
    User->>Langfuse: langfuse.prompt.get("extractor")
    Langfuse-->>User: extractorPrompt
    User->>Model: "model.bind({ functions, function_call }).pipe(JsonOutputFunctionsParser)"
    Model->>OpenAI: function_call request
    OpenAI-->>Model: raw function call response
    Model-->>User: parsed JSON via parser

    Note over User,OpenAI: After (current API)
    User->>Langfuse: langfuse.prompt.get("extractor")
    Langfuse-->>User: extractorPrompt
    User->>Model: "model.withStructuredOutput(schema, { method: "functionCalling" })"
    Model->>OpenAI: function calling request
    OpenAI-->>Model: structured response
    Model-->>User: parsed JSON (built-in)
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
sequenceDiagram
    participant User as User Code
    participant Langfuse
    participant Model as ChatOpenAI
    participant OpenAI as OpenAI API

    Note over User,OpenAI: Before (stale)
    User->>Langfuse: langfuse.prompt.get("extractor")
    Langfuse-->>User: extractorPrompt
    User->>Model: "model.bind({ functions, function_call }).pipe(JsonOutputFunctionsParser)"
    Model->>OpenAI: function_call request
    OpenAI-->>Model: raw function call response
    Model-->>User: parsed JSON via parser

    Note over User,OpenAI: After (current API)
    User->>Langfuse: langfuse.prompt.get("extractor")
    Langfuse-->>User: extractorPrompt
    User->>Model: "model.withStructuredOutput(schema, { method: "functionCalling" })"
    Model->>OpenAI: function calling request
    OpenAI-->>Model: structured response
    Model-->>User: parsed JSON (built-in)
Loading

Reviews (1): Last reviewed commit: "fix js langchain prompt cookbook structu..." | Re-trigger Greptile

@review-notebook-app

Copy link
Copy Markdown

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Jun 29, 2026
@vercel

vercel Bot commented Jun 29, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
langfuse-docs Ready Ready Preview, Comment Jun 29, 2026 10:54am

Request Review

@dosubot dosubot Bot added the docs label Jun 29, 2026
@github-actions

Copy link
Copy Markdown

@claude review

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — small docs cookbook fix moving from the deprecated bind/pipe parser to withStructuredOutput.

Extended reasoning...

Overview

This PR updates the JS LangChain prompt management cookbook (one .mdx + matching .ipynb) to replace the stale model.bind({functions, function_call}).pipe(new JsonOutputFunctionsParser()) pattern with model.withStructuredOutput(schema, { method: 'functionCalling' }), plus a heading rename to match the new API.

Security risks

None. This is a docs/cookbook change with no production code, no auth/crypto/permissions surface, and no new external network or filesystem behavior.

Level of scrutiny

Low. This is a documentation-only change in a cookbook example, mechanical in nature, and follows the now-standard LangChain JS API. The diff is small (~15 lines net), and the .mdx and .ipynb are kept in sync. The author also re-landed this carefully (per recent commit history: revert then re-apply), suggesting it was already vetted.

Other factors

A pre-existing nit was surfaced as an inline comment about the duplicate ## Example 1 heading — not a defect introduced by this PR, and not worth blocking. Vercel preview is green.

Comment thread content/guides/cookbook/js_prompt_management_langchain.mdx Outdated
@milanagm milanagm force-pushed the feature/lf-2221-update-js-langchain-prompt-management-cookbook-output-parser branch from f6c492d to f1330f7 Compare June 29, 2026 08:58
@milanagm milanagm force-pushed the feature/lf-2221-update-js-langchain-prompt-management-cookbook-output-parser branch from 04c55fb to 900a060 Compare June 29, 2026 10:48
@hassiebp hassiebp merged commit 41848db into main Jul 3, 2026
16 checks passed
@hassiebp hassiebp deleted the feature/lf-2221-update-js-langchain-prompt-management-cookbook-output-parser branch July 3, 2026 09:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants