fix(tools): handle malformed OpenAI tool arguments - #1488
Closed
abhinav7x94 wants to merge 1 commit into
Closed
Conversation
Member
|
Included in #1594 with your commit and authorship intact, plus a We dropped Thanks @abhinav7x94. |
graphite-app Bot
pushed a commit
that referenced
this pull request
Aug 24, 2026
#1594) Cherry-picks four contributor PRs for `@supermemory/tools` onto one branch, and bumps the package to 2.2.0. - #1244 (@rajarshidattapy): `withSupermemory` accepts `options.apiKey` instead of only reading `SUPERMEMORY_API_KEY`, matching the Vercel, Mastra and Voltagent integrations. Unblocks secrets managers, edge runtimes and per-request keys. - #1574 (@Agnik47): re-exports `PromptTemplate`, `MemoryPromptData` and `WithSupermemoryOptions` from `ai-sdk`. `./vercel` is not a published subpath, so the documented custom-template example did not compile. - #1488 (@abhinav7x94): malformed tool-call JSON returns an error result instead of throwing out of the request. - #1507 (@abhinav7x94): VoltAgent `onEnd` awaits the conversation save, which was fire-and-forget and could be dropped when a serverless runtime tore down. Dropped the `middleware.test.ts` added by #1244. Note that editing `packages/tools/package.json` triggers the npm publish workflow on merge. Co-Authored-By: rajarshidattapy <138959719+rajarshidattapy@users.noreply.github.com> Co-Authored-By: Agnik47 <140933190+Agnik47@users.noreply.github.com> Co-Authored-By: abhinav7x94 <204053250+abhinav7x94@users.noreply.github.com>
Agnik47
added a commit
to Agnik47/supermemory
that referenced
this pull request
Aug 28, 2026
…ect args
`getProfile`, `documentList` and `memoryForget` all declare `required: []`,
so a model may legitimately call them with no arguments at all. The OpenAI
API serialises that as `arguments: ""`, and `parseToolArguments` handed the
empty string straight to `JSON.parse`, so every no-argument call came back as
{"success":false,"error":"Invalid JSON arguments for getProfile"}
Those three tools were unreachable in their documented no-argument form.
The same gate also lets non-object JSON through. `"null"` parses cleanly and
then rejects in the destructuring parameter of every tool function --
`TypeError: Cannot destructure property 'containerTag' of 'object null'` --
which escapes `executeToolCall`, since it has no catch, and fails the whole
request. That is precisely the throw supermemoryai#1488 added this gate to contain. `"5"`
and `"\"text\""` are quieter but worse: they destructure to `undefined` and
call the API with no container tag at all.
Treat blank arguments as `{}`, and require the parsed value to be a non-null,
non-array object. Malformed JSON still returns the tool error supermemoryai#1488 added.
Adds eight regression tests. Six of them fail against the current
implementation -- two on the blank-argument path and four on the non-object
path, one carrying the raw TypeError. The two guard tests, malformed JSON and
an ordinary well-formed call, pass both before and after, so the behaviour
supermemoryai#1488 established is pinned rather than changed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
Why
OpenAI-compatible models can emit partial or invalid JSON in function arguments. The executor previously parsed outside its result flow, so a
SyntaxErrorescaped andPromise.alldiscarded sibling results.Validation
bunx vitest run src/openai/tools.test.ts src/tool-operations.test.ts --testTimeout 100000(16 passed)bun run buildbunx biome check packages/tools/src/openai/tools.ts packages/tools/src/openai/tools.test.tsgit diff --checkThe package-wide TypeScript command remains red on unrelated pre-existing dependency and example errors; it reports no errors in the new test or changed lines.