Skip to content

The agent that grows with you

fix(agent): only strip _moa_prepared_request when the live client is not the MoA facade

Teknium8369170feafa
diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.py
index cc357f20c8c..b1ef005713e 100644
--- a/agent/chat_completion_helpers.py
+++ b/agent/chat_completion_helpers.py
@@ -910,10 +910,14 @@ def _dispatch_nonstreaming_api_request(agent, api_kwargs: dict, *, make_client):
# dead-connection cleanup / fallback+restore), agent.client may
# become a native OpenAI client while agent.provider stays
# "moa". Pop the MoA-internal key so the native SDK does not
- # reject it as an unexpected kwarg. The MoAClient facade
- # already handles a missing key by falling through to its
- # normal resolution path. (#78382)
- api_kwargs.pop("_moa_prepared_request", None)
+ # reject it as an unexpected kwarg — but only when the live
+ # client is NOT the facade: the facade consumes the key, and
+ # stripping it there forces a wasteful duplicate reference
+ # fan-out (the facade re-prepares from scratch). Only the MoA
+ # facade's completions object exposes ``prepare()``. (#78382)
+ _completions = getattr(getattr(agent.client, "chat", None), "completions", None)
+ if not callable(getattr(_completions, "prepare", None)):
+ api_kwargs.pop("_moa_prepared_request", None)
return agent.client.chat.completions.create(**api_kwargs)
request_client = make_client("chat_completion_request")
return request_client.chat.completions.create(**api_kwargs)