Skip to content

fix(tools): handle malformed OpenAI tool arguments - #1488

Closed
abhinav7x94 wants to merge 1 commit into
supermemoryai:mainfrom
abhinav7x94:fix/tools-invalid-json-arguments
Closed

fix(tools): handle malformed OpenAI tool arguments#1488
abhinav7x94 wants to merge 1 commit into
supermemoryai:mainfrom
abhinav7x94:fix/tools-invalid-json-arguments

Conversation

@abhinav7x94

Copy link
Copy Markdown
Contributor

What changed

  • return the existing structured error shape when OpenAI tool arguments contain malformed JSON
  • keep one malformed call from rejecting an entire tool-call batch
  • add focused offline regression coverage

Why

OpenAI-compatible models can emit partial or invalid JSON in function arguments. The executor previously parsed outside its result flow, so a SyntaxError escaped and Promise.all discarded sibling results.

Validation

  • bunx vitest run src/openai/tools.test.ts src/tool-operations.test.ts --testTimeout 100000 (16 passed)
  • bun run build
  • bunx biome check packages/tools/src/openai/tools.ts packages/tools/src/openai/tools.test.ts
  • git diff --check

The 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.

@MaheshtheDev

Copy link
Copy Markdown
Member

Included in #1594 with your commit and authorship intact, plus a Co-Authored-By trailer in the description so the credit survives the squash merge.

We dropped tools.test.ts. The parseToolArguments change itself is unchanged.

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