Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions stackone_ai/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1301,13 +1301,10 @@ class StackOneLangChainTool(BaseTool):
args_schema: dict[str, Any] = args_json_schema # ty: ignore[invalid-assignment]

def _run(self, **kwargs: Any) -> Any:
# Drop unsupplied optionals. pydantic materialises every optional field
# as None and BaseTool passes them all through, and the API reads an
# explicit null as "required field missing" — which 400'd every single
# tool call made through this adapter.
supplied = {key: value for key, value in kwargs.items() if value is not None}
# args_schema is a JSON Schema dict, so BaseTool passes the model's arguments
# through untouched: an omitted optional stays absent, an explicit null stays null.
try:
return parent_tool.execute(supplied)
return parent_tool.execute(kwargs)
except (StackOneError, ValueError) as exc:
# LangChain's handle_tool_error only catches ToolException, so a
# StackOneError would kill the graph rather than reaching the
Expand Down
19 changes: 15 additions & 4 deletions tests/test_fetch_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2038,15 +2038,26 @@ def test_langchain_raises_tool_exception_carrying_the_response_body(self, monkey
tool.to_langchain()._run(path_id="x")
assert excinfo.value.status_code == 400

def test_langchain_does_not_forward_unsupplied_optionals_as_null(self, monkeypatch):
"""The API reads an explicit null as "required field missing", so this 400'd every call."""
@pytest.mark.parametrize(
("sent", "forwarded"),
[
pytest.param({"path_id": "x"}, {"path_id": "x"}, id="omitted-optional-stays-absent"),
pytest.param(
{"path_id": "x", "body_after": None},
{"path_id": "x", "body_after": None},
id="explicit-null-is-forwarded",
),
],
)
def test_langchain_forwards_the_model_arguments_as_sent(self, monkeypatch, sent, forwarded):
from stackone_ai.tools import StackOneMcpTool

seen: dict[str, object] = {}
tool, _ = self._failing_tool()
tool.parameters.properties["body_after"] = {"type": ["string", "null"]}
monkeypatch.setattr(StackOneMcpTool, "execute", lambda _self, args=None: seen.update(args=args) or {})
tool.to_langchain()._run(path_id="x", body_after=None)
assert seen["args"] == {"path_id": "x"}
tool.to_langchain().invoke({"name": tool.name, "args": sent, "id": "call-1", "type": "tool_call"})
assert seen["args"] == forwarded

def test_pydantic_ai_raises_model_retry(self, monkeypatch):
pytest.importorskip("pydantic_ai")
Expand Down
Loading