Repository navigation
feat: let mocks return ToolMessage or Command directly - #94
atishay-kasliwal wants to merge 3 commits into
Conversation
|
@atishay-kasliwal I found one correctness issue to address before merging: [P2] preserve structured content blocks in the In I reproduced this through a real content = [
{"type": "text", "text": "Two results"},
{"type": "image_url", "image_url": {"url": "https://example.test/fixture.png"}},
]
result = (content, {"rows": [1, 2]})
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 |
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>
b36c901 to
90305e4
Compare
|
@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 |
Checklist
[Unreleased]changelog entry for a user-visible change, or confirmed the change is exempt.Description
The real-tool path returns whatever
executereturns, but the mock path sends every result through_format_mock_result. A mock returning aToolMessageproduces aToolMessagewhose content is therepr()of the original, and aCommandnever reaches the graph. That rules out mocking tools that update state, set an artifact, or reportstatus="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_responseruns before formatting:Commandis returned as is.ToolMessageis returned with a missingnameortool_call_idfilled in, keeping itsstatusandartifact. It works on a copy, since a mock may return the same object on every call.(content, artifact)builds aToolMessagewithartifactwhen the tool declaresresponse_format="content_and_artifact". It reads this fromrequest.tool, so it doesn't depend ontools=being passed to the wrapper.The recorder stores the mock's raw return value, as the issue specifies. The declarative
status/artifactkeys 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.
InjectedToolCallIdis injected on the real execution path, after the wrapper, so a mock can't know the ID for aToolMessageinside itsCommand.ToolNodedoesn't validate what a wrapper returns, so in a real graph such aCommandgoes through without an error and leaves the model's tool call unanswered. The second commit fills an emptytool_call_idand a missingnameonToolMessages in aCommand'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, anupdategiven as(key, value)pairs, and aToolNodewith a custommessages_keyare 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) anduv run pytest tests-e2e/(15 passed) on Python 3.12, 3.13 and 3.14, with coverage above the 74% and 22% floors inci.yml. The first commit alone passes everything too (546).ruff check,ruff format --checkandmypy src/are clean, andsphinx-build -b html docsfinishes with no warnings.main'swrapper.py, including one that runs aCommandmock through a realStateGraphwithToolNode.Commandwith onlygoto, withgraph=Command.PARENT, a singleToolMessageinstead of a list, a call with no ID (an emptytool_call_idstays 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 emptytool_call_idwithNone. It's fixed and covered by a test.Fixes #64