How to safely handle aborted tool calls when using OpenAI Conversations API?

OpenAI requires every tool call to be followed by a corresponding tool output.
If a tool output is missing, the API throws the error:
No tool output found for function call.

I’m storing chat history using OpenAI’s conversations API. The problem arises when a request is aborted (e.g. backend crash, network cutoff, or user cancellation). In such cases, the tool input may already be stored in conversations, but the corresponding tool output is never recorded. This leaves the conversation in an invalid state and causes subsequent OpenAI calls to fail.

I’ve tried two approaches:

  1. Appending a synthetic tool output (e.g. “Request aborted”)
  2. Deleting the orphaned tool input from conversations

Both approaches work in theory, but using conversations introduces a race condition: sometimes the tool input has not yet been persisted when the abort handler runs, so I can’t find or clean it.

I’m using Vercel’s AI SDK and have tried handling this in onAbort, onFinish, and prepareStep, but I still can’t reliably access the tool call inputs from either the arguments or the conversation item list.

What is the recommended way to handle aborted tool calls when using OpenAI conversations, so the conversation does not become corrupted?

Does someone know how to handle this??

I only need to quote another forum thread…

If anyone else is running into this issue, I’ve opened an official
issue https://github.com/openai/openai-python/issues/3561
on the openai-python GitHub repository about this exact behavior.

Right now, if you abort a stream (stream.close()) after a tool call has been
yielded, the local client state and the server-side Conversation state get
completely out of sync, resulting in 400 errors on the next turn.

While there are some hacky client-side workarounds being suggested (like
manually verifying if the tool call was stored before aborting, or writing
custom exception handlers), this defeats the entire purpose of using the
server-side Conversations API. The SDK or the API itself should inherently
handle state consistency when a stream is interrupted. We shouldn’t have to
micromanage the storage state just to safely stop a generation.

If you are dealing with this bug, please go upvote or comment on the GitHub
issue linked above. Getting more visibility on the repo is the best way to get
the SDK maintainers to implement a native, permanent fix rather than leaving us
to write boilerplate workarounds!

@OpenAI_Support hi can we receive a feedback regarding this issue?

Hey @Husnain_Mustafa! Sorry for the delayed response, and thanks to everyone who shared testing and context here.

One option worth testing is tracking the in-progress tool call and its call_id in your app’s local state so you can submit a matching function_call_output during recovery. However, this alone may not resolve the timing issue you described with server-side conversation storage.

These guides explain the expected tool-call flow and conversation handling:

If this is still happening, please share a minimal current reproduction and SDK versions here, or contact OpenAI Support with request IDs and timestamps.

Farah