@devries-cameron20 / composio-1 / commits / 31315357d
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

fix(py): correct without_retries rationale, preserve strict validation, pin copy() contract (#3658)

Review follow-up to #3657 (squash-merged before these review fixes could land on it). No behaviour change to the retry scoping itself — this tightens correctness, docs, and test coverage around the `without_retries` mechanism. - **Correct the `without_retries` caching rationale.** The previous docstring justified caching with "each clone creates a new `ContextVar`, and dynamically-created `ContextVar`s are never garbage-collected." That is false for this code path: `request_ctx` is only ever `.get()`, never `.set()`, so it is reclaimed by refcounting on drop (measured: 500k get-only clones leak ~0 KiB; the CPython leak only occurs with `.set()`). Caching is kept because it avoids constructing a fresh `HttpClient` on every `execute`/`proxy` (the hottest path) — the real, stated justification now. - **Preserve `_strict_response_validation` on the clone.** The Stainless-generated `copy()` drops it, so the no-retry sibling silently reverted to the default (`False`) even when the parent had it enabled. The `copy()` override now threads it through `_extra_kwargs` alongside `provider`, so the sibling differs from the parent only in `max_retries`. - **Document the scope boundary.** The `without_retries` docstring now states that only `tools.execute` / `tools.proxy` are de-retried; other non-idempotent writes (`auth_configs.create`/`update`/`delete`, `mcp.update`/`delete`, `connected_accounts.delete`/`refresh`, `link.create`) keep the default retries (most are naturally idempotent on retry; the durable fix is backend idempotency keys). - **Add regression coverage:** the clone preserves `_strict_response_validation`, plus contract guards pinning the Stainless `copy`/`_extra_kwargs`/`with_options` internals the override relies on — so a `composio_client` regen that breaks the contract fails as an obvious assertion rather than a cryptic `TypeError`. - Minor: fix the `x-sdk-version` `"unknwon"` → `"unknown"` typo and tighten `te.Self` typing on the cache field/property. No changeset (Python-only change; changesets are TypeScript-only).

Alberto Schiabel committed Jun 25, 2026 at 02:46 UTC 31315357dd3a32d92723b690a09e20257b34c510
2 files changed +57 -7
python/composio/client/__init__.py
+22 -7
@@ -183,7 +183,7 @@ class HttpClient(BaseComposio, WithLogger):
183 },
184 )
185 # Lazily-built sibling client with retries disabled; see `without_retries`.
186 - self._without_retries: t.Optional["HttpClient"] = None
186 + self._without_retries: t.Optional[te.Self] = None
187
188 def copy( # type: ignore[override]
189 self,
@@ -201,7 +201,15 @@ class HttpClient(BaseComposio, WithLogger):
201 (e.g. ``with_options(max_retries=0)``).
202 """
203 return super().copy( # type: ignore[misc]
204 - _extra_kwargs={"provider": self.provider, **_extra_kwargs},
204 + _extra_kwargs={
205 + "provider": self.provider,
206 + # The generated `copy` does not re-pass `_strict_response_validation`,
207 + # so without this the clone would silently fall back to the default
208 + # (False) even when the original had it enabled — keeping the sibling
209 + # a faithful copy that differs from the parent only in `max_retries`.
210 + "_strict_response_validation": self._strict_response_validation,
211 + **_extra_kwargs,
212 + },
213 **kwargs,
214 )
215
@@ -211,7 +219,7 @@ class HttpClient(BaseComposio, WithLogger):
219 with_options = copy
220
221 @property
214 - def without_retries(self) -> "HttpClient":
222 + def without_retries(self) -> te.Self:
223 """
224 A cached sibling client that never retries requests.
225
@@ -219,9 +227,16 @@ class HttpClient(BaseComposio, WithLogger):
227 where a silent retry after a read timeout can duplicate a side effect
228 (e.g. send an email twice). Reads keep the default retry behaviour.
229
222 - The sibling is cached rather than rebuilt per call: each clone creates a
223 - new ``ContextVar``, and dynamically-created ``ContextVar``s are never
224 - garbage-collected.
230 + Scope: only ``tools.execute`` / ``tools.proxy`` route through this today.
231 + Other non-idempotent writes (``auth_configs.create`` / ``update`` /
232 + ``delete``, ``mcp.update`` / ``delete``, ``connected_accounts.delete`` /
233 + ``refresh``, ``link.create``) keep the default retries — most are
234 + naturally idempotent on retry, and the durable fix is backend-honoured
235 + idempotency keys.
236 +
237 + The sibling is cached rather than rebuilt per call so a fresh client is
238 + not constructed on every execute/proxy (the hottest path); its options
239 + never change, so one per client suffices.
240 """
241 if self._without_retries is None:
242 self._without_retries = self.with_options(max_retries=0)
@@ -240,4 +255,4 @@ class HttpClient(BaseComposio, WithLogger):
255 try:
256 request.headers["x-sdk-version"] = version("composio")
257 except Exception:
243 - request.headers["x-sdk-version"] = "unknwon"
258 + request.headers["x-sdk-version"] = "unknown"
python/tests/test_no_retry_writes.py
+35
@@ -7,12 +7,14 @@ duplicate the side effect (e.g. send an email twice). ``Tools.execute`` and
7 retry-disabled clone), while reads keep the default retry behaviour.
8 """
9
10 +import inspect
11 import typing as t
12 from unittest.mock import Mock, patch
13
14 import httpx
15 import pytest
16 from composio_client import DEFAULT_MAX_RETRIES, APIError
17 +from composio_client import Composio as BaseComposio
18
19 from composio.client import HttpClient
20 from composio.core.models.base import allow_tracking
@@ -85,6 +87,37 @@ class TestCopyOverride:
87 # Reads on the original client keep retrying.
88 assert client.max_retries == DEFAULT_MAX_RETRIES
89
90 + def test_clone_preserves_strict_response_validation(self) -> None:
91 + # The generated `copy` drops `_strict_response_validation`; the override
92 + # re-injects it so the sibling differs from the parent only in retries.
93 + client = HttpClient(
94 + provider="test",
95 + api_key="sk-test",
96 + base_url="https://backend.invalid",
97 + _strict_response_validation=True,
98 + )
99 +
100 + assert client.without_retries._strict_response_validation is True
101 +
102 +
103 +class TestStainlessCopyContract:
104 + """Pin the generated-client internals the ``copy()`` override depends on.
105 +
106 + These guard the contract so a ``composio_client`` regen that breaks it fails
107 + as an obvious assertion here rather than a cryptic ``TypeError`` raised deep
108 + inside ``with_options`` at runtime.
109 + """
110 +
111 + def test_base_copy_accepts_extra_kwargs(self) -> None:
112 + # The override threads `provider` (and `_strict_response_validation`)
113 + # through `_extra_kwargs`; the base `copy` must still accept it.
114 + assert "_extra_kwargs" in inspect.signature(BaseComposio.copy).parameters
115 +
116 + def test_with_options_is_aliased_to_our_copy_override(self) -> None:
117 + # The base binds `with_options = copy` at class-definition time, so the
118 + # subclass must re-alias it to the override that re-injects `provider`.
119 + assert HttpClient.with_options is HttpClient.copy
120 +
121
122 class TestWritePathDoesNotRetry:
123 """``execute`` / ``proxy`` must reach the transport exactly once."""
@@ -124,6 +157,8 @@ class TestWritePathDoesNotRetry:
157 client = _client_with_transport(handler)
158 tools = Tools(client=client, provider=Mock())
159
160 + # `proxy` does no tool-schema lookup, so the only transport hit is the
161 + # write itself — no read to patch out (unlike the execute test above).
162 with pytest.raises(APIError):
163 tools.proxy(endpoint="/any", method="POST")
164