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): read blocked responses whose nested fields are client models
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Mukund committed
Oct 20, 2026 at 10:21 UTC
bd1430ab0e521571c98bb74674e4156d2fc00dd6
2 files changed
+57
-2
python/composio/core/models/tools.py
+17
-2
@@ -210,13 +210,28 @@ def parse_blocked_response(
210
if getattr(response, "result_type", None) != "blocked":
211
return None
212
try:
213
- return BlockedResponse.model_validate(dict(response))
213
+ return BlockedResponse.model_validate(
214
+ {key: _plain(value) for key, value in dict(response).items()}
215
+ )
216
except PydanticValidationError as exc:
217
raise ValidationError(
218
f"{subject} returned a malformed blocked response"
219
) from exc
220
221
222
+def _plain(value: t.Any) -> t.Any:
223
+ """Nested client models as plain data.
224
+
225
+ The generated client may return ``reason`` and ``hook_results`` as its own
226
+ nested models; the SDK validates plain data against its own types.
227
+ """
228
+ if isinstance(value, PydanticBaseModel):
229
+ return value.model_dump()
230
+ if isinstance(value, list):
231
+ return [_plain(item) for item in value]
232
+ return value
233
+
234
+
235
def _response_field(response: t.Any, name: str) -> t.Any:
236
"""A field of a generated client response model, declared or not.
237
@@ -237,7 +252,7 @@ def parse_hook_results(
252
253
:raises ValidationError: If ``hook_results`` does not match its shape.
254
"""
240
- raw = _response_field(response, "hook_results")
255
+ raw = _plain(_response_field(response, "hook_results"))
256
if raw is None:
257
return None
258
try:
python/tests/test_tool_router.py
+40
@@ -2940,6 +2940,46 @@ class TestSessionHooks:
2940
{"id": "block_injection", "status": "unavailable"}
2941
]
2942
2943
+ def test_blocked_response_from_typed_client_models(self):
2944
+ """A regenerated client returns ``reason`` and ``hook_results`` as its
2945
+ own nested models instead of dicts."""
2946
+ from composio.core.models.tools import parse_blocked_response
2947
+
2948
+ class _Reason(BaseModel):
2949
+ type: str
2950
+ hook_id: str
2951
+
2952
+ class _HookResult(BaseModel):
2953
+ id: str
2954
+ status: str
2955
+ outcome: t.Optional[str] = None
2956
+
2957
+ class _Blocked(BaseModel):
2958
+ result_type: str
2959
+ reason: _Reason
2960
+ message: str
2961
+ hook_results: t.List[_HookResult]
2962
+
2963
+ blocked = parse_blocked_response(
2964
+ _Blocked(
2965
+ result_type="blocked",
2966
+ reason=_Reason(type="request_blocked", hook_id="no_external_email"),
2967
+ message="Blocked by session policy",
2968
+ hook_results=[
2969
+ _HookResult(
2970
+ id="no_external_email", status="evaluated", outcome="block"
2971
+ )
2972
+ ],
2973
+ ),
2974
+ "Tool GMAIL_SEND_EMAIL",
2975
+ )
2976
+
2977
+ assert blocked is not None
2978
+ assert blocked.reason.hook_id == "no_external_email"
2979
+ assert blocked.hook_results == [
2980
+ {"id": "no_external_email", "status": "evaluated", "outcome": "block"}
2981
+ ]
2982
+
2983
def test_malformed_blocked_response_is_raised(self):
2984
malformed = {k: v for k, v in _BLOCKED_CALL_JSON.items() if k != "reason"}
2985
client, _ = _answering_client(malformed)