Skip to content

Replay the phase of assistant messages over the Responses API, add GenerateOptions::store - #71

Merged
alexey-milovidov merged 1 commit into
mainfrom
responses-message-phase
Dec 5, 2026
Merged

alexey-milovidov merged 1 commit into
mainfrom
responses-message-phase

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

Related: ClickHouse/ClickHouse#136766

The phase of assistant messages. The Responses API gives an assistant message a phase: commentary for a preamble before tool calls, final_answer for the answer. The guide of OpenAI says that a client that replays the history by itself has to preserve the phase of every message, and that a dropped phase can make a preamble be taken for the final answer.

The Responses client replays the history by itself, and it lost the phase: the parser reduced a message to its text, and the request builder rebuilt the assistant messages without the field.

Now the parser records the phase of a message as a ProviderContentPart ({"type": "message", "phase": ...}) right before the text of the message, next to the reasoning items that it records already. The request builder sends the text that follows such a part as a message of its own with that phase. A message without a phase is built as before, and other providers ignore the part, as they ignore the reasoning items.

GenerateOptions::store. A Responses API request always said store: false, and a Chat Completions request could not say it at all, while OpenAI stores Chat Completions by default for new accounts. The new option lets the caller say it. Nothing changes when it is not set: a Responses API request says false, and a Chat Completions request does not say, because a compatible service may not know the parameter.

Tests: OpenAIResponsesParserTest.KeepsThePhaseOfAMessageBeforeItsText, OpenAIResponsesRequestTest.ReplaysThePhaseOfAssistantMessages, MessagesWithoutPhaseStayAsTheyWere, StoresOnlyWhenTheCallerSays, OpenAIClientTest.SaysWhetherToStoreOnlyWhenTheCallerDoes.

🤖 Generated with Claude Code


Note

Medium Risk
Changes multi-turn tool-loop request shape for GPT-6 Responses clients; incorrect phase replay could alter model behavior, but scope is limited to OpenAI Responses parsing/building with solid test coverage.

Overview
Fixes OpenAI Responses API history replay by preserving assistant message phase (commentary vs final_answer) so pre-tool preamble is not mistaken for the final answer. The parser now emits a ProviderContentPart with {"type":"message","phase":...} immediately before the message text; the request builder turns the following text into a separate assistant message that includes that phase, alongside existing reasoning replay.

Adds optional GenerateOptions::store: Responses requests still default to store: false but honor store: true when set; Chat Completions only sends store when the caller sets it, avoiding breakage on compatible APIs. Docs and unit tests cover phase round-trip, mixed phased/unphased content, and store behavior for both builders.

Reviewed by Cursor Bugbot for commit b5f7935. Bugbot is set up for automated code reviews on this repo. Configure here.

…enerateOptions::store`

The Responses API gives an assistant message a `phase`: `commentary` for a
preamble before tool calls, `final_answer` for the answer. A client that
replays the history by itself has to send each message back with its phase:
without it a preamble can be taken for the final answer.

The parser reduced a message to its text, and the request builder rebuilt the
assistant messages without the field. Now the parser records the phase of a
message as a `ProviderContentPart` right before the text of the message, next
to the `reasoning` items that it records already, and the builder sends the
text that follows such a part as a message of its own with that phase. A
message without a phase is built as before.

Add `GenerateOptions::store`. A Responses API request always said
`store: false`, and a Chat Completions request could not say it at all, while
OpenAI stores Chat Completions by default for new accounts. Nothing changes
when the option is not set: a Responses API request says `false`, and a Chat
Completions request does not say.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
alexey-milovidov added a commit to ClickHouse/ClickHouse that referenced this pull request Dec 4, 2026
…es API

The Responses API of OpenAI gives a message of the model a `phase`: `commentary` for a preamble
before tool calls, `final_answer` for the answer. The AI agent of the client replays the whole
conversation with every request, and it has to send each message back with its phase: without it
a preamble can be taken for the final answer. The phase was lost: the SDK reduced a message to its
text.

- The `contrib/ai-sdk-cpp` submodule is moved to ClickHouse/ai-sdk-cpp#71,
  which is one commit on top of the `main` of the SDK. Its parser records the phase of a message as
  a part of the provider right before the text of the message, and its request builder sends the
  text back as a message with that phase.
- `AIAgent::makeAssistantMessage` kept the text of a step once, where it began. A step with two
  messages would give the text of both the phase of the first, so every text stays where it is, and
  the texts share the cap of a step.
- The answer of a turn was kept as a text alone. It is kept with the parts of the provider as
  well, like a step with tool calls, so that it goes back as `final_answer`.
- Another model gets the conversation without the phases, as it gets it without the `reasoning`
  items: `? model` drops what only the provider understands.

The SDK change also adds `GenerateOptions::store`, and a Chat Completions request to OpenAI itself
says `store: false` now, as a Responses API request does. A compatible service at a custom
`base_url` is not told, because it may refuse a parameter that it does not know. The documentation
says so.

#136766 (comment)
#136766 (comment)

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@alexey-milovidov alexey-milovidov self-assigned this Dec 5, 2026

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ok.

@alexey-milovidov
alexey-milovidov merged commit 16941ea into main Dec 5, 2026
3 checks passed
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.

1 participant