Skip to content

feat: let mocks return ToolMessage or Command directly - #94

Open
atishay-kasliwal wants to merge 3 commits into
intuit:mainfrom
atishay-kasliwal:feature/64-mock-toolmessage-command
Open

atishay-kasliwal wants to merge 3 commits into
intuit:mainfrom
atishay-kasliwal:feature/64-mock-toolmessage-command

Conversation

@atishay-kasliwal

Copy link
Copy Markdown
Contributor

Checklist

  • I've read and agree to the project's contribution guidelines.
  • I'm requesting to pull a topic/feature/bugfix branch.
  • I checked that my code additions will pass code linting checks and unit tests.
  • I updated unit and integration tests (if applicable).
  • I added an [Unreleased] changelog entry for a user-visible change, or confirmed the change is exempt.
  • I'm ready to notify the team of this contribution.

Description

The real-tool path returns whatever execute returns, but the mock path sends every result through _format_mock_result. A mock returning a ToolMessage produces a ToolMessage whose content is the repr() of the original, and a Command never reaches the graph. That rules out mocking tools that update state, set an artifact, or report status="error" without raising.

This is two commits. The first is the issue as written. The second is an addition, kept separate so it can be dropped without touching the first.

Commit 1: what #64 asks for. _build_tool_response runs before formatting:

  • Command is returned as is.
  • ToolMessage is returned with a missing name or tool_call_id filled in, keeping its status and artifact. It works on a copy, since a mock may return the same object on every call.
  • (content, artifact) builds a ToolMessage with artifact when the tool declares response_format="content_and_artifact". It reads this from request.tool, so it doesn't depend on tools= being passed to the wrapper.
  • Anything else is formatted as before, including tuples for other tools.

The recorder stores the mock's raw return value, as the issue specifies. The declarative status/artifact keys for data-driven cases are left for the follow-up the issue mentions.

Commit 2: beyond the issue. A mock only receives the tool's arguments. InjectedToolCallId is injected on the real execution path, after the wrapper, so a mock can't know the ID for a ToolMessage inside its Command. ToolNode doesn't validate what a wrapper returns, so in a real graph such a Command goes through without an error and leaves the model's tool call unanswered. The second commit fills an empty tool_call_id and a missing name on ToolMessages in a Command's update: one message or a list under "messages" in a dict update, or a list update. IDs the mock sets are kept. Messages written as dicts, an update given as (key, value) pairs, and a ToolNode with a custom messages_key are returned unchanged, and the docs say so. If you'd prefer to keep this PR to the issue, the first commit stands alone and I'll drop the second.

Verification

  • uv run pytest (550 passed) and uv run pytest tests-e2e/ (15 passed) on Python 3.12, 3.13 and 3.14, with coverage above the 74% and 22% floors in ci.yml. The first commit alone passes everything too (546).
  • ruff check, ruff format --check and mypy src/ are clean, and sphinx-build -b html docs finishes with no warnings.
  • 11 of the new tests fail against main's wrapper.py, including one that runs a Command mock through a real StateGraph with ToolNode.
  • I probed a few extra shapes: Command with only goto, with graph=Command.PARENT, a single ToolMessage instead of a list, a call with no ID (an empty tool_call_id stays empty), and a mock that sets its own ID. This turned up one bug in an earlier draft, where a missing call ID overwrote an empty tool_call_id with None. It's fixed and covered by a test.

Fixes #64

@Bbenchaya

Copy link
Copy Markdown
Collaborator

@atishay-kasliwal I found one correctness issue to address before merging: [P2] preserve structured content blocks in the (content, artifact) path.

In src/stuntdouble/wrapper.py:341-345, _build_tool_response unpacks the tuple and then always passes its content through _format_mock_result, which JSON-serializes lists.

I reproduced this through a real StateGraph / ToolNode with a tool declared using response_format="content_and_artifact" and this return value:

content = [
    {"type": "text", "text": "Two results"},
    {"type": "image_url", "image_url": {"url": "https://example.test/fixture.png"}},
]
result = (content, {"rows": [1, 2]})
  • The real tool preserves the list in ToolMessage.content, including the image block.
  • The mock returns a JSON string as content; the image becomes text. The artifact is preserved correctly.
  • Returning an explicit ToolMessage with the same blocks works, so the gap is specific to the tuple branch.

Could you preserve valid content-block lists in that branch while keeping the existing formatting for ordinary mock values, and add a graph regression test comparing mocked and real content?

The combined version with current main passes all 613 existing unit/e2e tests, Ruff checks, and mypy; this case is currently uncovered. Thanks!

The mock path passed every result through _format_mock_result, so a mock
returning a ToolMessage produced a ToolMessage whose content was the
repr() of the original, and a Command never reached the graph. Mocks
could not stand in for tools that update state, set an artifact, or
report status="error" without raising.

A Command is now returned as is. A ToolMessage is returned with a missing
name or tool_call_id filled in, keeping its status and artifact. A
(content, artifact) tuple builds a ToolMessage with an artifact when the
tool declares response_format="content_and_artifact".

Fixes intuit#64

Signed-off-by: Atishay Kasliwal <katishay@gmail.com>
A mock only receives the tool's arguments, so it cannot see the tool call
ID that a real tool gets through InjectedToolCallId, and ToolNode does
not validate what a wrapper returns. A ToolMessage inside a mocked
Command's update therefore kept an empty tool_call_id and never answered
the model's call.

Fill in an empty tool_call_id and a missing name on ToolMessages in a
Command's update: one message or a list under "messages" in a dict
update, or a list update. IDs the mock sets are kept, and messages
written as dicts or an update given as (key, value) pairs are returned
unchanged.

Refs intuit#64

Signed-off-by: Atishay Kasliwal <katishay@gmail.com>
A tool declared with response_format="content_and_artifact" keeps a list of
content blocks as the ToolMessage content, but the mock path JSON-encoded it,
so an image block came back as text. Keep a list langchain_core would keep
and format everything else as before.

Signed-off-by: Atishay Kasliwal <katishay@gmail.com>
@atishay-kasliwal
atishay-kasliwal force-pushed the feature/64-mock-toolmessage-command branch from b36c901 to 90305e4 Compare November 23, 2026 15:29
@atishay-kasliwal

Copy link
Copy Markdown
Contributor Author

@Bbenchaya thanks, fixed in 90305e4. In the tuple branch the content is now kept when it's a list langchain_core would keep (strings or dicts whose type is in TOOL_MESSAGE_BLOCK_TYPES), and anything else is formatted as before. Your case is now a StateGraph/ToolNode test that compares the mocked message with the real tool's: content, artifact and tool_call_id. I also rebased onto main. 618 unit and e2e tests pass on 3.12 and 3.14, along with ruff and mypy.

This branch has not been deployed

No deployments
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.

feat: allow mocks to return ToolMessage or Command directly instead of being stringified

2 participants