Skip to content

fix(tools): forward explicit nulls through the LangChain adapter - #259

Closed
glebedel wants to merge 2 commits into
mainfrom
s2d2/audit-langchain-nulls
Closed

glebedel wants to merge 2 commits into
mainfrom
s2d2/audit-langchain-nulls

Conversation

@glebedel

@glebedel glebedel commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

What

StackOneTool.to_langchain() no longer drops None arguments in _run. The LangChain adapter now forwards exactly what the model sent, the same as to_pydantic_ai_tool already does.

Why

The filter was added in #199, when args_schema was a pydantic model: BaseTool then filled every unsupplied optional with None, so dropping None was the only way to tell "omitted" from "sent". Since the toolset became MCP-only, args_schema is the served JSON Schema dict. With a dict schema BaseTool passes the tool-call args through untouched, so an omitted optional is already absent. The filter now only removes something real: a null the model sent on purpose (for example to clear a nullable field), which the server never sees.

Probe against the locked langchain-core (1.6.3):

Model sent _run received before _run receives now
{"path_id": "x"} {"path_id": "x"} {"path_id": "x"}
{"path_id": "x", "body_after": null} {"path_id": "x"} {"path_id": "x", "body_after": null}

Risk

A caller that relied on LangChain stripping explicit nulls will now send them. Header arguments are unaffected: a null header value is still dropped by the header sanitiser before the call.

How verified

  • Replaced the old test (which asserted the drop) with a parametrized test_langchain_forwards_the_model_arguments_as_sent that invokes the converted tool with a real tool call; the null case fails on main and passes here.
  • uv run ruff check ., uv run ruff format --check ., uv run ty check stackone_ai examples, uv run pytest -q: 872 passed.

Gates (head c67101b)

  • Forge: ran, slop meter +17 -10, no findings.
  • Security scan: ran, traced None through the nested and flat header sanitising paths; nulls are dropped there, other arguments were already forwarded verbatim.
  • Codex security review: ran, no findings.
  • /security-review: ran on the same scope, no findings.

Requested by guillaume@stackone.com in Slack (repo audit thread)

🤖 Generated with Claude Code


Summary by cubic

Makes the LangChain adapter forward explicit nulls to the tool call instead of dropping them, matching what to_pydantic_ai_tool already does. Previously every None argument was filtered because a pydantic args schema materialised unsupplied optionals as None; now the schema is a JSON Schema dict, so BaseTool passes the tool-call arguments through untouched — an omitted optional stays absent, and a null the model sent on purpose reaches the server.

  • Replaces the old test (which asserted the drop) with parametrized tests invoking the converted tool with a real tool call; the null case fails on main and passes here.

Risk

  • Callers that relied on LangChain stripping explicit nulls will now send them.
  • Header arguments are unaffected: a null header value is still dropped by the header sanitiser before the call.

Written for commit ab6f751. Summary will update on new commits.

View guided diff Turn on auto-fix

to_langchain() dropped every None argument because, with the old pydantic
args_schema, BaseTool materialised each unsupplied optional as None. The
schema has been a plain JSON Schema dict since the MCP-only toolset, and
on langchain-core 1.6.3 BaseTool passes exactly what the model sent: an
omitted optional is absent, an explicit null is null. The filter now only
removed nulls the model chose to send, which the OpenAI and Pydantic AI
adapters forward. Forward the arguments as given.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Guillaume Lebedel <10488548+glebedel@users.noreply.github.com>
@glebedel glebedel self-assigned this Nov 10, 2026
Two lines of comment now carry the whole contract, and the two test cases are
named by pytest ids instead of inline comments. No behaviour change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Guillaume Lebedel <10488548+glebedel@users.noreply.github.com>
@glebedel

Copy link
Copy Markdown
Contributor Author

Closing: the /code-review pass on ab6f751 showed this change is wrong.

  • CLAUDE.md states the rule: optional wire keys are omitted, never null. Forwarding a model's explicit null breaks it on the LangChain route, while toolset.execute() still drops them.
  • The note for refactor!: MCP-only toolset with account discovery, search/execute, and hardened security #199 records that the API reads an explicit null as "required field missing" (10 of 10 list tools returned 400 through the adapter). With a JSON Schema dict, BaseTool no longer fills in nulls, but models that fill unused optionals with null (strict-mode tool calls, for example) still send them, and the filter is what keeps those calls working.
  • The PR body's history was wrong: the dict args_schema and the null drop arrived in the same squash commit (e53edf0), so the filter was never only a pydantic workaround.

The filter on main stays. Its comment still says pydantic materialises the None values, which is no longer the reason; fixing that wording is a one-line follow-up if wanted.

Requested by guillaume@stackone.com in Slack (repo audit thread)

@glebedel glebedel closed this Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants