Skip to content

fix(openai): interleave reasoning blocks with paired function calls in conversation history#971

Open
Cbrad24 wants to merge 1 commit intoprism-php:mainfrom
Cbrad24:fix/openai-interleave-reasoning-with-function-calls
Open

fix(openai): interleave reasoning blocks with paired function calls in conversation history#971
Cbrad24 wants to merge 1 commit intoprism-php:mainfrom
Cbrad24:fix/openai-interleave-reasoning-with-function-calls

Conversation

@Cbrad24
Copy link

@Cbrad24 Cbrad24 commented Mar 23, 2026

Fixes #955.

When using OpenAI provider, with OpenAI reasoning enabled models (e.g. gpt-5-mini) with tool calling and conversational history, and a reasoning model decides to plan and execute a multi-tool calling chain (e.g. ListTenants -> ListTenantUsers(tenantId)), the GPT 5 models expect you to return reasoning items that are immediately followed by a paired function_call, and the assistant message last.

This PR reworks the order of returned messages to match that:

Before (current Prism code):
1. { role: "assistant", content: [{ type: "output_text", text: "I found 5 users in tenant..." }] }
2. { type: "reasoning", id: "rs_...fb60..." }
3. { type: "reasoning", id: "rs_...0054..." }
4. { type: "function_call", name: "ListTenants", id: "fc_...fe05..." }
5. { type: "function_call", name: "ListTenantUsers", id: "fc_...0325..." }

OpenAI sees rs_1 followed by rs_2 — rejects it  with error 400 because rs_1 needs its function_call immediately after.

The fix:
1. { type: "reasoning", id: "rs_...fb60..." }
2. { type: "function_call", name: "ListTenants", id: "fc_...fe05..." }
3. { type: "reasoning", id: "rs_...0054..." }
4. { type: "function_call", name: "ListTenantUsers", id: "fc_...0325..." }
5. { role: "assistant", content: [{ type: "output_text", text: "I found 5 users in tenant..." }] }

…g items are sent in the correct order. Update tests to confirm mapping is as expected.
@Cbrad24 Cbrad24 changed the title [OpenAI] Interleave reasoning blocks with paired function calls in conversation history fix(openai): interleave reasoning blocks with paired function calls in conversation history Mar 24, 2026
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.

[OpenAI] Reasoning items break multi-turn conversation history

1 participant