@devries-cameron20 / composio-1 / commits / 95b7b7f64
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(openai_agents): deep-copy tool schema before stripping examples/default (#3632)

## Problem `OpenAIAgentsProvider.wrap_tool` mutates the caller's `Tool.input_parameters`. It shallow-copies the schema and then strips keys from the nested property dicts **in place**: ```python modified_schema = tool.input_parameters.copy() # shallow copy modified_schema["additionalProperties"] = False _remove_examples_from_schema(modified_schema) # del prop["examples"/"pattern"/"default"] ``` `_remove_examples_from_schema` recurses into `schema["properties"][*]` and does `del prop_value["examples"]`, `del prop_value["pattern"]`, `del prop_value["default"]`. Because `dict.copy()` is shallow, `modified_schema["properties"]` (and every nested property dict) is the **same object** as the original, so those deletions strip `examples`/`pattern`/**`default`** out of the caller's `Tool.input_parameters`. Since essentially every Composio tool carries `default`/`examples` on its properties, wrapping a tool silently corrupts its schema. Wrapping the same `Tool` twice, reusing it with another provider, or inspecting it after wrapping then sees the defaults gone. This is the only wrap path using a shallow `.copy()` + nested mutation — the rest of the codebase deep-copies before mutating schemas, e.g. `composio/utils/shared.py` (`schema = copy.deepcopy(schema)`, with the docstring *"The schema is deep-copied before any mutation so the caller's original is never modified."*) and `Tools._get` (`tool.model_copy(deep=True)`). ## Fix Deep-copy the schema before mutating (`copy.deepcopy(tool.input_parameters)`), so `_remove_examples_from_schema` only edits the provider-local copy. One-line change + `import copy`. ## Test Added `python/providers/openai_agents/tests/test_provider.py::test_wrap_tool_does_not_mutate_input_parameters` — builds a tool whose properties carry `default`/`examples`/`pattern`, snapshots `input_parameters`, calls `wrap_tool`, and asserts both sides of the contract: - the caller's original `Tool.input_parameters` is unchanged - the returned `FunctionTool.params_json_schema` is still normalized for OpenAI Agents (`additionalProperties: false`, with `default`/`examples`/`pattern` stripped) - **Before:** the assertion fails — `owner` loses `default`/`examples`, `repo` loses `pattern`. - **After:** `Tool.input_parameters` is untouched while the wrapped schema remains sanitized. Verification: - `uv run pytest python/providers/openai_agents/tests/test_provider.py -q` → **1 passed** - `uv run ruff check python/providers/openai_agents/composio_openai_agents/provider.py python/providers/openai_agents/tests/test_provider.py` → clean - `uv run ruff format --check python/providers/openai_agents/composio_openai_agents/provider.py python/providers/openai_agents/tests/test_provider.py` → clean Co-authored-by: ly-wang19 <ly-wang19@users.noreply.github.com>

ly-wang19 committed Jun 25, 2026 at 05:59 UTC 95b7b7f649cef98cd42c84c642fa91b6b2c9bc13
2 files changed +58 -2
python/providers/openai_agents/composio_openai_agents/provider.py
+7 -2
@@ -1,4 +1,5 @@
1 import asyncio
2 +import copy
3 import json
4 import typing as t
5
@@ -110,8 +111,12 @@ class OpenAIAgentsProvider(
111 )
112
113 # Ensure the schema has additionalProperties set to false
113 - # this is required by OpenAI's function validation
114 - modified_schema = tool.input_parameters.copy()
114 + # this is required by OpenAI's function validation.
115 + # Deep-copy so the in-place edits below (additionalProperties + the
116 + # examples/pattern/default removal) never mutate the caller's
117 + # Tool.input_parameters, matching the deepcopy contract in
118 + # composio.utils.shared.
119 + modified_schema = copy.deepcopy(tool.input_parameters)
120 modified_schema["additionalProperties"] = False
121
122 # Apply the example removal function. This is done to optimize the
python/providers/openai_agents/tests/test_provider.py new
+51
@@ -0,0 +1,51 @@
1 +"""Tests for the OpenAI Agents provider."""
2 +
3 +import copy
4 +from unittest.mock import MagicMock
5 +
6 +from composio_openai_agents.provider import OpenAIAgentsProvider
7 +
8 +
9 +def test_wrap_tool_does_not_mutate_input_parameters():
10 + """wrap_tool must not strip default/examples/pattern from the caller's schema.
11 +
12 + ``wrap_tool`` removes ``examples``/``pattern``/``default`` from the schema it
13 + hands to the OpenAI Agents SDK. It must do so on a *copy*: a shallow
14 + ``dict.copy()`` left the nested ``properties`` dicts shared with the original,
15 + so those in-place deletions leaked back into the caller's
16 + ``Tool.input_parameters``.
17 + """
18 + tool = MagicMock(
19 + slug="GITHUB_GET_REPO",
20 + name="Github Get Repo",
21 + description="Get a repository",
22 + input_parameters={
23 + "type": "object",
24 + "properties": {
25 + "owner": {"type": "string", "default": "me", "examples": ["octocat"]},
26 + "repo": {"type": "string", "pattern": "^[a-z]+$"},
27 + },
28 + "required": ["owner"],
29 + },
30 + )
31 + snapshot = copy.deepcopy(tool.input_parameters)
32 +
33 + wrapped_tool = OpenAIAgentsProvider().wrap_tool(tool, lambda **kwargs: {})
34 +
35 + # The caller's schema must be untouched.
36 + assert tool.input_parameters == snapshot
37 + owner = tool.input_parameters["properties"]["owner"]
38 + assert owner["default"] == "me"
39 + assert owner["examples"] == ["octocat"]
40 + assert tool.input_parameters["properties"]["repo"]["pattern"] == "^[a-z]+$"
41 +
42 + # The provider-local schema should still be normalized for OpenAI Agents.
43 + assert wrapped_tool.params_json_schema == {
44 + "type": "object",
45 + "properties": {
46 + "owner": {"type": "string"},
47 + "repo": {"type": "string"},
48 + },
49 + "required": ["owner"],
50 + "additionalProperties": False,
51 + }