Skip to content

The agent that grows with you

fix: apply the per-model image strip to the max-iterations summary request

kshitijk4poorfe4294802e13
diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.py
index 315641d14cf..9ff1591cf67 100644
--- a/agent/chat_completion_helpers.py
+++ b/agent/chat_completion_helpers.py
@@ -42,7 +42,7 @@ from agent.message_content import flatten_message_text
from agent.message_metadata import append_message, stamp_message_timestamp
from agent.message_sanitization import (
_sanitize_surrogates, _repair_tool_call_arguments, normalize_finish_reason as _normalize_finish_reason,
- sanitize_outbound_kwargs,
+ sanitize_outbound_kwargs, strip_images_for_rejecting_model,
)
from agent.reasoning_summaries import append_streamed_reasoning_detail, separate_glued_reasoning_blocks
from agent.stream_single_writer import claim_stream_writer, stream_writer_is_current
@@ -2180,6 +2180,12 @@ def _iteration_summary_api_messages(agent, messages: list) -> list:
# Same send-path vision eviction as the main loop (#89296).
from agent.context_compressor import evict_stale_outbound_tool_images
evict_stale_outbound_tool_images(api_messages)
+ # Same per-model image strip as turn_api_request.build_api_request: this path builds
+ # api_messages by hand and calls _build_api_kwargs directly, so a model recorded in
+ # agent._image_rejecting_models would otherwise get images here → 4xx → no summary.
+ # Safe on the shallow row copies: the strip rebinds the row's ``content``, never the
+ # nested list shared with history.
+ strip_images_for_rejecting_model(agent, api_messages)
# Thinking-only assistant turns 400 on Anthropic-family providers; _thinking_prefill must
# survive until here so the drop pass recognizes stubs after reasoning is stripped.
api_messages = agent._drop_thinking_only_and_merge_users(api_messages)
diff --git a/tests/agent/test_image_rejection_fallback.py b/tests/agent/test_image_rejection_fallback.py
index 59f7bf6ff55..3c6727b2886 100644
--- a/tests/agent/test_image_rejection_fallback.py
+++ b/tests/agent/test_image_rejection_fallback.py
@@ -435,3 +435,33 @@ class TestRejectionNeverReachesPersistedHistory:
api_messages = self._history()
assert strip_images_for_rejecting_model(agent, api_messages) is False
assert str(api_messages).count("data:image/png") == 2
+
+
+def test_iteration_summary_strips_images_for_rejecting_model(tmp_path, monkeypatch):
+ """The max-iterations summary hand-builds api_messages and bypasses build_api_request, so
+ it must apply the same per-model strip; history keeps its image."""
+ import copy
+
+ from agent.chat_completion_helpers import _iteration_summary_api_messages
+ from agent.vision_message_prep import _provider_model_key
+ from run_agent import AIAgent
+
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ agent = AIAgent(api_key="k", base_url="https://api.groq.com/openai/v1", provider="custom", model="m",
+ quiet_mode=True, skip_context_files=True, skip_memory=True)
+ agent._cached_system_prompt = "SYS"
+ agent._image_rejecting_models.add(_provider_model_key(agent))
+ history = [
+ {"role": "user", "content": [
+ {"type": "text", "text": "look"},
+ {"type": "image_url", "image_url": {"url": "data:image/png;base64,AAAA"}},
+ ]},
+ {"role": "assistant", "content": "ok"},
+ ]
+ before = copy.deepcopy(history)
+
+ out = _iteration_summary_api_messages(agent, history)
+
+ assert "image_url" not in str(out), "summary request must be text-only for a rejecting model"
+ assert any("look" in str(m.get("content")) for m in out if m.get("role") == "user")
+ assert history == before, "the per-call strip must not leak into canonical history"