Skip to content

The agent that grows with you

fix(agent): relay-managed aux streams run the SDK bypass inside the provider callback

kshitijk4poor8bca1f7167c9
diff --git a/agent/auxiliary_client.py b/agent/auxiliary_client.py
index 2bb21c60942..6b2921dd5de 100644
--- a/agent/auxiliary_client.py
+++ b/agent/auxiliary_client.py
@@ -2632,10 +2632,12 @@ def _relay_sync_stream(
) -> Any:
from agent.auxiliary_wire import prepare_chat_messages
- kwargs = bypass_chat_sdk_request_transform(prepare_chat_messages(client, kwargs), client)
+ kwargs = prepare_chat_messages(client, kwargs)
route = _relay_auxiliary_metadata(provider=provider, api_mode=api_mode)
if route is None:
- return client.chat.completions.create(**kwargs)
+ return client.chat.completions.create(**bypass_chat_sdk_request_transform(kwargs, client))
+ # The bypass runs inside the provider callback, AFTER Relay has seen (and possibly
+ # rewritten) the real conversation; hoisting it here would hand Relay an empty one.
provider_name, fallback_model, metadata = route
from agent import relay_llm
return relay_llm.stream_current(
@@ -6865,14 +6867,15 @@ def _create_with_progress_once(
# reset the compression inactivity fence, or a zero-output attempt runs to the
# total ceiling instead of idling out (#114938). Progress ticks only for
# substantive stream payloads or a completed usable response.
+ kwargs = bypass_chat_sdk_request_transform(kwargs, client)
if (not _aux_progress_active() and not force_stream) or _client_streams_internally(client):
- response = client.chat.completions.create(**bypass_chat_sdk_request_transform(kwargs, client))
+ response = client.chat.completions.create(**kwargs)
if not _client_streams_internally(client):
_notify_aux_provider_response()
return response
stream_kwargs, model, total_ceiling = _stream_request_plan(kwargs)
try:
- chunks = client.chat.completions.create(**bypass_chat_sdk_request_transform(stream_kwargs, client))
+ chunks = client.chat.completions.create(**stream_kwargs)
except Exception as exc:
# Genuine provider failures aren't streaming's fault — surface unchanged so the
# recovery chains see the same error as a plain call.
@@ -6884,7 +6887,7 @@ def _create_with_progress_once(
logger.debug("Auxiliary %s: streamed request failed (%s); retrying non-streaming",
task or "call", exc)
_notify_aux_dispatch()
- response = client.chat.completions.create(**bypass_chat_sdk_request_transform(kwargs, client))
+ response = client.chat.completions.create(**kwargs)
_notify_aux_provider_response()
return response
# Some shims (MoA quiet mode, defensive adapters) return a complete response despite
@@ -7090,14 +7093,15 @@ async def _acreate_with_progress(
chunk) when a progress hook is active or the provider is stream-only; plain create otherwise."""
_notify_aux_dispatch()
# Same contract as the sync twin (#114938): dispatch alone is not progress.
+ kwargs = bypass_chat_sdk_request_transform(kwargs, client)
if (not _aux_progress_active() and not force_stream) or _async_client_streams_internally(client):
- response = await client.chat.completions.create(**bypass_chat_sdk_request_transform(kwargs, client))
+ response = await client.chat.completions.create(**kwargs)
if not _async_client_streams_internally(client):
_notify_aux_provider_response()
return response
stream_kwargs, model, total_ceiling = _stream_request_plan(kwargs)
try:
- chunks = await client.chat.completions.create(**bypass_chat_sdk_request_transform(stream_kwargs, client))
+ chunks = await client.chat.completions.create(**stream_kwargs)
except Exception as exc:
# Only a rejected stream NEGOTIATION falls back to a plain call (mirrors the sync wrapper); a
# failure mid-consumption below reaches the classified recovery ladder instead of silently
@@ -7108,7 +7112,7 @@ async def _acreate_with_progress(
logger.debug("Auxiliary %s: streamed async request failed (%s); retrying non-streaming",
task or "call", exc)
_notify_aux_dispatch()
- response = await client.chat.completions.create(**bypass_chat_sdk_request_transform(kwargs, client))
+ response = await client.chat.completions.create(**kwargs)
_notify_aux_provider_response()
return response
if hasattr(chunks, "choices"): # shims may hand back a complete response despite stream=True
diff --git a/tests/agent/test_chat_sdk_transform_bypass.py b/tests/agent/test_chat_sdk_transform_bypass.py
index 18bac38a4a5..16da6a9b09a 100644
--- a/tests/agent/test_chat_sdk_transform_bypass.py
+++ b/tests/agent/test_chat_sdk_transform_bypass.py
@@ -149,3 +149,28 @@ def test_iteration_summary_path_hands_the_sdk_only_the_placeholder(monkeypatch):
assert seen[0]["messages"] == [] and seen[0]["tools"] == []
assert seen[0]["extra_body"]["messages"] == body["messages"]
assert seen[0]["extra_body"]["tools"] == body["tools"]
+
+
+def test_relay_stream_path_shows_relay_the_full_conversation(monkeypatch):
+ """On the Relay-managed stream path the bypass must run INSIDE the provider callback: Relay's
+ tracing/intercepts see the real ``messages`` while the SDK still gets only the placeholder."""
+ from agent import auxiliary_client, relay_llm
+
+ seen = _capture_sdk_create(monkeypatch)
+ client = _Recorder().client
+ body = {"model": "m", "messages": [{"role": "user", "content": "x" * 4096}], "stream": True}
+ relay_saw: list[dict] = []
+
+ def fake_stream_current(request, provider_call, **_kw):
+ relay_saw.append(dict(request))
+ return provider_call(request)
+
+ monkeypatch.setattr(relay_llm, "stream_current", fake_stream_current)
+ monkeypatch.setattr(
+ auxiliary_client, "_relay_auxiliary_metadata", lambda **_kw: ("openrouter", "m", {}))
+
+ auxiliary_client._relay_sync_stream(client, dict(body))
+
+ assert relay_saw[0]["messages"] == body["messages"]
+ assert seen[0]["messages"] == []
+ assert seen[0]["extra_body"]["messages"] == body["messages"]