Attach a leading `CachePoint` to the previous message on OpenAI instead of raising

Merged
DO
2mo ago
+284-6

On GPT-5.6 and GPT-6, where explicit prompt-cache breakpoints are on, a CachePoint that opened a user message raised UserError: CachePoint cannot be the first content in a user message. That's exactly where a tail reminder puts its breakpoint once a tool loop is underway. So Harness SystemReminders with OpenAIChatModel('gpt-5.6') crashed the run the first time a reminder fired after a tool call (gpt-5.5, without explicit breakpoints, completed).

Anthropic and Bedrock already handle this by moving the breakpoint onto the previous message, since "cache everything up to here" means the same there. OpenAI now does the same, on Chat Completions and Responses:

  • A leading CachePoint leaves an empty placeholder part.
  • Once the request is mapped, _move_leading_cache_breakpoints puts the breakpoint on the last content part of the closest earlier item that has content: a tool result, a function call output, or a user or assistant message.
  • A string body becomes a single text part to carry it. The text is unchanged.
  • A CachePoint with no earlier content anywhere still raises, as on Anthropic. The two existing tests for that still pass.

Live verification (GPT-5.6, real API)

Both APIs accept prompt_cache_breakpoint on a tool result's content (a Chat tool message's content parts, a Responses function_call_output's output), and validate it: an unknown key or mode: 'nope' gets a 400. With SystemReminders(reminders=[Reminder(..., interval=1)]) in a four-call tool loop, the run completes, and each request's cache read covers the previous request's input apart from the reminder:

openai-chat:gpt-5.6       in=4305 read=   0 | 4437 read=4281 | 4569 read=4413 | 4701 read=4545 | 4833 read=4677
openai-responses:gpt-5.6  in=4304 read=   0 | 4535 read=4280 | 4676 read=4511 | 4845 read=4652 | 4977 read=4821

The tool result carries the breakpoint as a one-part list in the request where the reminder follows it, and as a plain string in later requests. The reads above show that change doesn't move the cached prefix.

Tests: test_openai_chat_leading_cache_point_attaches_to_previous_message, ..._skips_messages_without_content and test_openai_responses_leading_cache_point_attaches_to_previous_item fail on main. They catch dropping the string-body conversion, dropping the list-body path, keeping the emptied user message, not raising when nothing precedes, and missing the output key on Responses.

This also unblocks the cache-safe Planning reminder in #9408, which will lead with a CachePoint after tool results the same way.

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver.
  • PR title is fit for the release changelog.

šŸ¤– Generated with Claude Code