Skip to content

fix(openai): include Responses API instructions in captured input#754

Open
mmahmed wants to merge 3 commits into
langfuse:mainfrom
mmahmed:fix/openai-responses-instructions
Open

fix(openai): include Responses API instructions in captured input#754
mmahmed wants to merge 3 commits into
langfuse:mainfrom
mmahmed:fix/openai-responses-instructions

Conversation

@mmahmed

@mmahmed mmahmed commented Mar 20, 2026

Copy link
Copy Markdown

Summary

When using the OpenAI Responses API with observeOpenAI(), the instructions parameter (system prompt) is not captured in the generation input. This PR fixes parseInputArgs to merge instructions into the input, mirroring how chat.completions captures system messages in the prompt.

This is the JS/TS equivalent of the Python SDK fix in langfuse-python#1565.

Changes

packages/openai/src/parseOpenAI.ts — 19 lines added to parseInputArgs:

  • When instructions is present alongside input (string), merge into [{role: system}, {role: user}] message list
  • When instructions is present alongside input (array), prepend {role: system} entry
  • When only instructions is present (no input), return { instructions }
  • Non-string instructions are ignored (no-op)

packages/openai/src/parseOpenAI.test.ts — new unit test file:

  • 9 tests covering all input combinations: string, array, missing, non-string instructions, chat completions, and completions API paths

Behavior

instructions input Before After
"You are a pirate." "Hello!" "Hello!" [{role: "system", content: "You are a pirate."}, {role: "user", content: "Hello!"}]
"You are a pirate." [{role: "user", content: "Hello!"}] [{role: "user", ...}] [{role: "system", content: "You are a pirate."}, {role: "user", ...}]
"You are a pirate." (none) undefined {instructions: "You are a pirate."}
(none) "Hello!" "Hello!" "Hello!" (unchanged)

Test plan

  • 9 unit tests pass (vitest run packages/openai/src/parseOpenAI.test.ts)
  • Matches behavior of the merged Python SDK fix (langfuse-python#1565)

Fixes langfuse/langfuse#9775

Disclaimer: Experimental PR review

Greptile Summary

This PR fixes a gap in the OpenAI integration where the instructions parameter (system prompt) from the Responses API was silently dropped and never captured in Langfuse's generation input. The fix extends parseInputArgs in parseOpenAI.ts to detect and merge instructions into the input field — as a [{role: "system"}, {role: "user"}] message list for string/array inputs, or as a plain { instructions } object when no input is provided — mirroring the existing behaviour for Chat Completions system messages.

Key points:

  • The logic correctly handles all four input shape variants: string input, array input, absent input, and unknown-type input.
  • The instructions block runs before the existing messages/prompt checks; if both messages and instructions are present (unusual but possible), the instructions-merged result is silently overwritten — adding a !("messages" in args) guard would make the precedence explicit.
  • 9 new unit tests cover the main paths well, but a test for the messages + instructions combination is absent, leaving the interaction behaviour undocumented.

Confidence Score: 4/5

  • Safe to merge; the fix is correct for all realistic Responses API use cases with only minor defensive hardening opportunities remaining.
  • The core logic is sound and matches the intent of the Python SDK fix. The two concerns (lack of messages guard and missing edge-case test) are low-risk given that Responses API and Chat Completions API are always called on separate endpoints, making the overlap scenario unrealistic in production.
  • No files require special attention; both changed files are self-contained and isolated to the parsing layer.

Important Files Changed

Filename Overview
packages/openai/src/parseOpenAI.ts Adds instructions-merge logic for the Responses API to parseInputArgs; logic is correct for all realistic API paths but lacks a guard against the messages block silently overwriting the merged input when both messages and instructions are present.
packages/openai/src/parseOpenAI.test.ts New test file with 9 well-structured unit tests covering all documented input combinations; missing a test for the instructions + messages combination, which would lock in the expected precedence behavior.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[parseInputArgs called] --> B{instructions present\nand is string?}
    B -- No --> G
    B -- Yes --> C{typeof input}
    C -- string --> D["Merge: system msg + user msg array"]
    C -- array --> E["Prepend system msg to array"]
    C -- falsy --> F["input = instructions object"]
    C -- other object --> H["input = instructions + input object"]
    D --> G{messages in args?}
    E --> G
    F --> G
    H --> G
    G -- Yes --> I["input = messages block\nWARN: overwrites instructions merge"]
    G -- No --> J{input still falsy?}
    J -- Yes --> K["input = args.prompt"]
    J -- No --> L[Return model + input + modelParameters]
    I --> L
    K --> L
Loading

Last reviewed commit: "fix(openai): include..."

Greptile also left 1 inline comment on this PR.

(2/5) Greptile learns from your feedback when you react with thumbs up/down!

When using the OpenAI Responses API, the `instructions` parameter
(system prompt) was not captured in the generation input. This merges
`instructions` into the input, mirroring how chat.completions captures
system messages in the prompt.

Fixes langfuse/langfuse#9775
Equivalent of langfuse/langfuse-python#1565 for the JS/TS SDK.
@vercel

vercel Bot commented Mar 20, 2026

Copy link
Copy Markdown

@mmahmed is attempting to deploy a commit to the langfuse Team on Vercel.

A member of the Team first needs to authorize it.

@CLAassistant

CLAassistant commented Mar 20, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Comment thread packages/openai/src/parseOpenAI.ts
mmahmed and others added 2 commits March 20, 2026 16:01
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
@hassiebp hassiebp self-requested a review April 7, 2026 15:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: instructions parameter (OpenAI Responses API) not captured/displayed properly

2 participants