[BUG] TS retrieve_context returns error text with status "success" (Python returns "error")
Checks
- [x] I have searched issues and there are no duplicates
SDK Language
TypeScript
Strands Version
main (a642b4177), also present on #4228 head 70cefbd12
Steps to Reproduce
- Agent with
ContextManager(stash enabled); model callsretrieve_contextwith a reference that isn't in the stash, or passespatternfor a media ref. - Inspect the resulting
ToolResultBlock.
Expected Behavior
status: "error", as Python does at strands-py/src/strands/_context_manager/retrieval_tool.py:68,84,93,104.
Actual Behavior
strands-ts/src/context-manager/retrieval-tool.ts:67 and :82 return bare strings (Error: reference not found: K, Error: cannot search non-text content…), so FunctionTool._wrapInToolResult wraps them in a success tool result. Observed in a mock-model e2e run: status=success text="Error: reference not found: tu-old_0". A model seeing success + "Error" tends to retry, then hallucinate the content.
Suggested fix
Throw (the tool factory converts a throw into an error result) or return a ToolResultBlock with status: 'error', and make the text terminal ("content unavailable — do not retry").
Found while reviewing #4228 (pre-existing, not introduced there). Filed by strandly-the-agent.
I'd like to pick this up if no one is already working on it. I'd keep the change to the two failure branches in retrieve_context, throwing so the existing tool wrapper returns status: 'error' with the same error text, and add coverage through .stream(). Successful retrievals and searches with no matches would stay successful. Direct .invoke() calls would throw for those failures instead of returning error strings. Does that scope sound right?
@lizradway
I will take this issue. Please assign it to me.
The problem is that retrieve_context in TypeScript returns error text with a "success" status. This happens at strands-ts/src/context-manager/retrieval-tool.ts:67 and :82. I will first inspect how errors are handled in the Python version at strands-py/src/strands/_context_manager/retrieval_tool.py. Then, I will adjust the TypeScript code to either throw an error or return a ToolResultBlock with status: 'error'. This should align the behavior with the Python implementation.
Hi @modelpath-dev, have you started a patch for this? I posted a proposed scope above and am waiting for maintainer feedback on the change to direct .invoke() calls. Happy to coordinate so we don't duplicate the work.
Hi @rioyu123 and @modelpath-dev, I'd be happy to collaborate and learn more about this part of Strands. I could help with regression tests for the error-result wrapping path and successful retrieval/no-match cases, once the scope is agreed with maintainers. Is there a small testing or validation task I could take? I haven't run the tests yet and would use AI assistance.
@BayesLee thanks for jumping in, that would really help! One useful starting point is an independent reproduction on current main: check how a missing reference and a pattern search on non-text content come back through the tool-result wrapping (status and content), with a successful retrieval and a no-match search as controls. If you can share the commit you tested on and what you see, that would give us all a solid baseline.
We still don't have maintainer input on whether direct .invoke() should throw instead of returning an error string, and I haven't heard back from @modelpath-dev on coordination yet, so it probably makes sense to hold off on an implementation PR until that's clearer.
Thanks, @rioyu123. I’d be happy to help with the independent reproduction. I haven’t run it yet. I’ll report the tested commit and status/content for the two failure cases, with successful retrieval and no-match search as controls. I’ll hold off on implementation changes until the direct .invoke() behavior and coordination are agreed.
Hi @rioyu123, I independently reproduced both cases on main commit 79cd7356d3947e2f1bdfa65529938f80aad03b87 using the real retrieval tool, Stash and InMemoryStorage. Only the ToolContext uses the existing upstream fixture.
| Case | Actual stream status | Expected status |
|---|---|---|
| Missing reference | success, with "Error: reference not found: nonexistent" | error |
| Pattern search on an image reference | success, with the non-text search error message | error |
| Full text retrieval | success, with the original text | success |
| Search with no matches | success, with the no-matches message | success |
I collected the async generator's final return value, rather than only its yielded events. All four results are ToolResultBlock instances with TextBlock content. I also recorded the raw .invoke() results separately; those do not have a status field.
The four existing Node test files for retrieval, stash, FunctionTool and ZodTool pass: 105 tests, with retries disabled. The existing retrieval tests assert the error text through .invoke(), which explains why they pass while the stream status issue remains reproducible.
This verifies the tool stream wrapper, not a full Agent/model run, and I have not changed the SDK implementation. Before working on a fix, could a maintainer confirm the intended .invoke() behavior for these failures, especially whether it should throw while .stream() emits an error result? I am happy to help with the focused regression tests within the agreed scope.
Hi @rioyu123, following up on the reproduction results above. Would focused regression tests be useful while we wait for maintainer guidance on .invoke() behavior? I'd be happy to prepare them without changing the implementation.
Hi @rioyu123, I've uploaded the runnable reproduction here: https://github.com/BayesLee/harness-sdk/tree/6c77ce2f7a56f703dccd8e26530b91564b8b06ad/.pr/strands-4241
I reran the portable package against the pinned SDK baseline 79cd7356 on October 14. The missing-reference and image-pattern cases still report .stream() status success; the full-text and no-match controls pass. Baseline mode exits 0, while contract mode exits 1 and records the two status mismatches in fresh JSON.
The added files contain reproduction material only; the SDK implementation is unchanged. Prepared with AI assistance. I'd be happy to coordinate focused regression tests once maintainers confirm the failure contract and collaboration scope.
Thanks @BayesLee. I read reproduce.mts and run.py at 6c77ce2 (SDK pinned at 79cd735), though I haven't run them. Using collectGenerator to get the generator's final return value makes the status checks easy to follow, and the success and no-match controls help.
Focused tests sound useful to me. They could assert an error status for the missing reference and for the non-text pattern search, and check that a successful retrieval and a no-match search still succeed with the same content. A test-only branch would give everyone something concrete to look at.
I'd leave the direct .invoke() expectations open until a maintainer confirms that contract. I'd also hold off on implementation changes until that's settled and coordinated with modelpath-dev.
Hi @rioyu123, the test-only regression draft is now available here: https://github.com/BayesLee/harness-sdk/commit/fa9ec79bf08f8a926d6eef4757f733b481d74eac
It adds four .stream() cases covering missing references, image-pattern searches, successful full-text retrieval, and searches with no matches. On pinned SDK baseline 79cd7356, the focused four-file Node run reports 107 passed and 2 failed. Both failures are status-only mismatches: expected error, received success; the complete content and tool-use IDs match.
The original .invoke() tests and SDK implementation are unchanged. Formatting, ESLint, and TypeScript checks pass. Browser tests could not run because Chromium is unavailable, and I have not validated the full published branch or current main. I have not opened a PR. I'd appreciate a review of the test scope; implementation changes remain on hold pending maintainer guidance and coordination. Prepared with AI assistance.
Thanks @BayesLee, this keeps the scope nicely focused. I applied just the test diff from fa9ec79 on top of 79cd7356 and ran the four related Node test files on Windows. There were 105 passing before the patch. Afterward, 107 passed and the two new error-status cases failed as expected: unknown reference, and pattern search on image content. In both, the text and toolUseId match and only error vs success differs. The full-text and no-match cases pass. Prettier and ESLint on the changed file, build, and type-check also passed locally. I haven't run the browser tests or checked current main.
Because the two error cases will stay red until the implementation changes, I'd treat this as a reproduction rather than something to merge on its own. It also leaves the existing .invoke() tests and SDK code untouched. I'd hold off on the fix until a maintainer confirms how direct .invoke() should behave for these cases, and until it's coordinated with modelpath-dev.
Still on this. Thanks @BayesLee and @rioyu123 for the repro and the test draft. Once a maintainer confirms that a failed retrieve_context should come back with an error status, I'll put up the fix on top of those tests.
Thanks, @modelpath-dev, sounds good. I'll keep my implementation on hold. One thing it'd help to settle at the same time: whether direct .invoke() should keep returning error strings, since switching it to throw would change that contract. Happy to help check the fix against BayesLee's tests once it's up.