Skip to content

The agent that grows with you

fix(bedrock): reasoning GPTs replay only the reasoning they stamped on Converse

unsupportedpastels32b7af1bab30
diff --git a/agent/bedrock_adapter.py b/agent/bedrock_adapter.py
index 2a266fd706c..69ce78feb31 100644
--- a/agent/bedrock_adapter.py
+++ b/agent/bedrock_adapter.py
@@ -864,16 +864,19 @@ def _assistant_blocks(msg: dict, content, claude: bool = True, own: bool = False
# the signature and all previous messages"; Bedrock ignores older turns server-side for free).
# strip — DeepSeek-R1 (AWS's multi-turn sample removes reasoningContent) and Kimi K3 (Converse raises
# InternalServerException "when reasoning content from earlier turns is included"): prior turns
-# lose reasoning, the in-flight loop replays the model's own blocks as captured. Also OpenAI's
-# reasoning GPTs (gpt-6 family): they reject readable reasoningText in assistant history too
-# ("This model doesn't support the reasoningContent.reasoningText.text field").
+# lose reasoning, the in-flight loop replays the model's own blocks as captured.
+# issuer — OpenAI's reasoning GPTs (gpt-6 family) reject readable reasoningText in assistant history ("This
+# model doesn't support the reasoningContent.reasoningText.text field") and stall on another GPT's
+# sealed blobs: only turns the answering model stamped keep reasoning, verbatim so the replayed
+# prefix stays stable; every other turn replays none, in the in-flight loop too.
# unsigned — every other model: prior turns carry readable reasoningText only, since their signatures and
# redacted blobs may be another model's (a /model switch from Claude) and non-Claude models
# reject them ("This model doesn't support the reasoningContent.reasoningText.signature field");
# the in-flight loop was produced by the model now answering and replays as captured.
# A turn stamped by another model (CONVERSE_ISSUER_TYPE) never gets the in-flight verbatim replay: a fallback or
# switch mid-loop hands the loop to a model that cannot open the previous one's sealed blobs (_reasoning_filter).
-_CONVERSE_REPLAY_STRIP_PATTERNS = ("deepseek.r1", "deepseek-r1", "moonshotai.kimi-k3", "openai.gpt-6")
+_CONVERSE_REPLAY_STRIP_PATTERNS = ("deepseek.r1", "deepseek-r1", "moonshotai.kimi-k3")
+_CONVERSE_REPLAY_ISSUER_PATTERNS = ("openai.gpt-6",) # not gpt-oss: it takes readable reasoningText
# reasoning_details record naming the model that minted a turn's Converse reasoning: its signatures are that
@@ -896,6 +899,8 @@ def _converse_replay_policy(model: Optional[str]) -> str:
lowered = model.lower()
if "claude" in lowered:
return "signed"
+ if any(p in lowered for p in _CONVERSE_REPLAY_ISSUER_PATTERNS):
+ return "issuer"
return "strip" if any(p in lowered for p in _CONVERSE_REPLAY_STRIP_PATTERNS) else "unsigned"
@@ -919,6 +924,7 @@ _REASONING_FILTERS = { # policy -> (before the in-flight tool loop, inside it)
"signed": (_verbatim, _verbatim),
"strip": (_without_reasoning, _verbatim),
"unsigned": (_unsigned_reasoning, _verbatim),
+ "issuer": (_without_reasoning, _without_reasoning),
}
@@ -931,11 +937,14 @@ def _reasoning_filter(policy: str, issuer: Optional[str], own: bool, *, in_loop:
model (fallback or ``/model`` switch, including mid tool loop) loses the in-flight loop's verbatim replay:
sealed ``redactedContent`` opens only for its issuer, and Claude rejects another family's blobs outright
(``Invalid `data` in `redacted_thinking` block``), so a non-Claude turn replays no reasoning to Claude.
- Unstamped turns (older sessions) keep the policy, and the rejection-driven resend covers them."""
+ Unstamped turns (older sessions) keep the policy, and the rejection-driven resend covers them; under
+ ``issuer`` only the answering model's own stamped turns keep any reasoning."""
prior, loop = _REASONING_FILTERS[policy]
- if issuer is not None and not own:
+ if own:
+ return _verbatim if in_loop or policy in ("unsigned", "issuer") else prior
+ if issuer is not None:
return _without_reasoning if policy == "signed" and _converse_replay_policy(issuer) != "signed" else prior
- return loop if in_loop or (own and policy == "unsigned") else prior
+ return loop if in_loop else prior
def convert_messages_to_converse(
diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.py
index da4b46616fc..243b7fbf7b0 100644
--- a/agent/chat_completion_helpers.py
+++ b/agent/chat_completion_helpers.py
@@ -747,8 +747,9 @@ def _bedrock_converse_call(api_kwargs: dict, *, stream: bool, on_stream_denied=N
try:
raw_response = method(**api_kwargs)
except Exception as exc:
- retry_kwargs = recover_from_cache_point_rejection(exc, api_kwargs)
- if retry_kwargs is not None or (retry_kwargs := recover_from_redacted_reasoning_rejection(exc, api_kwargs)):
+ retry_kwargs = (recover_from_cache_point_rejection(exc, api_kwargs)
+ or recover_from_redacted_reasoning_rejection(exc, api_kwargs))
+ if retry_kwargs is not None:
return finish(method(**retry_kwargs))
if on_stream_denied is not None and is_streaming_access_denied_error(exc):
return on_stream_denied(client, api_kwargs, exc)
diff --git a/tests/agent/test_bedrock_converse_reasoning_replay.py b/tests/agent/test_bedrock_converse_reasoning_replay.py
index 138d574e13b..5b4a97154cb 100644
--- a/tests/agent/test_bedrock_converse_reasoning_replay.py
+++ b/tests/agent/test_bedrock_converse_reasoning_replay.py
@@ -5,7 +5,7 @@ error, so a Claude turn captured in Anthropic shape (``anthropic_content_blocks`
thinking ahead of its toolUse. AWS docs: Claude needs the signature and all previous messages; DeepSeek-R1's
sample removes prior reasoning; Kimi K3 raises InternalServerException on prior-turn reasoning; non-Claude
models reject the ``reasoningText.signature`` field; OpenAI's reasoning GPTs (gpt-6) reject ``reasoningText``
-in assistant history outright.
+in assistant history outright, so they get back only the reasoning they stamped themselves.
"""
from __future__ import annotations
@@ -66,9 +66,6 @@ def _reasoning(blocks):
("global.moonshotai.kimi-k3", [], [{"text": "own sig_loop"}]),
# gpt-oss rejects reasoningText.signature: prior turns go back as readable text only.
("openai.gpt-oss-120b-1:0", [{"text": "own sig_prior"}], [{"text": "own sig_loop"}]),
- # Reasoning GPTs reject readable reasoningText in assistant history outright (#140177): prior
- # turns lose their reasoning; the in-flight loop still replays the model's own blocks.
- ("global.openai.gpt-6.1-sol", [], [{"text": "own sig_loop"}]),
],
)
def test_converse_replays_each_models_reasoning_contract(model, prior, loop):
@@ -171,17 +168,19 @@ def _replayed_reasoning(kwargs):
@pytest.mark.parametrize("reloaded", [False, True])
@pytest.mark.parametrize("issuer, target, expect", [
- # The issuing model gets its own sealed blobs back, prior turn and in-flight loop alike.
+ # The issuing model gets its own reasoning back verbatim, prior turn and in-flight loop alike, so the
+ # replayed prefix never changes under it.
(GPT, GPT, [["reasoningText", "redactedContent"]] * 2),
# Fallback or switch to Claude, including mid-loop: Claude cannot read another family's reasoning at all.
(GPT, OPUS, [[], []]),
- # Another GPT cannot open the blob either (it hangs on it): readable text only, in the loop too.
- (GPT, "global.openai.gpt-6-sol", [["reasoningText"]] * 2),
+ # Another GPT stalls on the blob and rejects readable reasoningText: nothing, in the loop too.
+ (GPT, "global.openai.gpt-6-sol", [[], []]),
+ # The reverse fallback: GPT rejects Claude's reasoningText in assistant history (#140177).
+ (OPUS, GPT, [[], []]),
])
-def test_reasoning_another_model_minted_never_replays_sealed(issuer, target, expect, reloaded):
- """Invariant: a sealed redactedContent blob reaches only the model that minted it. Coatue field report:
- a GPT-6.1 session fell back to Opus 5.5 after two read timeouts and every call 400'd on
- ``Invalid `data` in `redacted_thinking` block`` because GPT's blobs were replayed verbatim."""
+def test_cross_model_replay_drops_reasoning_the_target_cannot_take(issuer, target, expect, reloaded):
+ """Invariant: a sealed redactedContent blob reaches only the model that minted it, and a reasoning GPT
+ replays no reasoning it did not mint (a fallback in either direction otherwise fails every retry)."""
history = _session(issuer, reloaded=reloaded)
before = copy.deepcopy(history)
kwargs = BedrockTransport().build_kwargs(model=target, messages=history)
@@ -197,3 +196,10 @@ def test_claude_minted_reasoning_still_replays_to_claude():
history = _session("global.anthropic.claude-sonnet-5-5", reloaded=False)
kwargs = BedrockTransport().build_kwargs(model=OPUS, messages=history)
assert _replayed_reasoning(kwargs) == [["reasoningText", "redactedContent"]] * 2
+
+
+def test_reasoning_gpt_replays_no_unstamped_reasoning_mid_loop():
+ """The AnthropicBedrock path stamps nothing, so a Claude → GPT fallback inside Claude's tool loop must
+ still drop Claude's thinking from the in-flight turn, not replay it as readable reasoningText."""
+ kwargs = BedrockTransport().build_kwargs(model=GPT, messages=_history())
+ assert "reasoningContent" not in repr(kwargs["messages"])