Skip to content
Open
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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ const response = await client.responses.create({
input: 'Are semicolons optional in JavaScript?',
});

### Multi-turn conversations and reasoning items
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Close the TypeScript code block before new README section

The ### Multi-turn conversations and reasoning items heading is currently inside the preceding ```ts block, so Markdown renders the new guidance as code and makes the example snippet invalid when copied. This defeats the purpose of the documentation update because users won’t see it as prose and may paste broken sample code; insert a closing code fence before this heading.

Useful? React with 👍 / 👎.


When using the Responses API, reasoning and message items must be preserved as pairs.

A common mistake is filtering `response.output` to keep only messages.
This can lead to 400 errors due to missing reasoning items.

Recommended:
- Pass `response.output` directly, OR
- Preserve reasoning-message pairs when constructing history

console.log(response.output_text);
```

Expand Down
42 changes: 42 additions & 0 deletions examples/reasoning-safe-history.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import OpenAI from "openai";

const client = new OpenAI();

let conversation = [];

function preservePairs(output) {
const result = [];

for (let i = 0; i < output.length; i++) {
const curr = output[i];
const next = output[i + 1];

if (curr.type === "reasoning" && next?.type === "message") {
result.push(curr, next);
i++;
}
}

return result;
}

async function run() {
for (const msg of [
"Write a Python prime checker.",
"Add type hints.",
"Add docstrings."
]) {
conversation.push({ role: "user", content: msg });

const response = await client.responses.create({
model: "gpt-5.3-codex",
input: conversation,
reasoning: { effort: "high" },
});

// ✅ SAFE
conversation.push(...preservePairs(response.output));
}
}

run();