fix(parsing): handle empty function tool call arguments#3502
Open
sohumt123 wants to merge 1 commit into
Open
Conversation
The API returns empty-string arguments (rather than "{}") for strict
function tools that take no arguments or whose parameters are all
optional. `parse_function_tool_arguments` passed that empty string
straight to `json.loads`/`model_parse_json`, which raised
`json.JSONDecodeError` (or a pydantic `ValidationError`) on the
non-streaming parse and done/final streaming paths.
The SDK's own incremental streaming parser already guards this case on
the delta path, but the shared helpers used by `chat.completions.parse`,
`responses.parse`, and the streaming done events did not, so the
exception leaked out of the stream iterator before `get_final_completion`.
Normalize empty/whitespace-only arguments to "{}" in both the chat and
responses helpers before parsing. Non-empty arguments are unaffected.
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.
This change is entirely within
src/openai/lib/andtests/lib/, whichCONTRIBUTING.mdstates the generator will never modify ("The generator will never modify the contents of thesrc/openai/lib/andexamples/directories."), so it should be safe from being overwritten on the next generation.Changes being requested
parse_function_tool_arguments()raises when a strict function tool call has empty-string arguments.Problem
The API returns empty-string arguments (rather than
"{}") for strict function tools that take no arguments or whose parameters are all optional. Both copies ofparse_function_tool_arguments()pass that empty string straight to the parser:src/openai/lib/_parsing/_completions.py—json.loads(function.arguments)for strict tools andmodel_parse_json(input_fn.model, function.arguments)forPydanticFunctionToolsrc/openai/lib/_parsing/_responses.py— the same two paths withfunction_call.argumentsjson.loads("")raisesjson.JSONDecodeError: Expecting value: line 1 column 1 (char 0), andmodel_parse_json(model, "")raises a pydanticValidationError. This affectschat.completions.parse(),responses.parse(), and the streaming done events, so withchat.completions.stream()the exception leaks out of the stream iterator beforeget_final_completion()can be reached.Interestingly, the SDK's own incremental streaming parser already guards this exact case on the delta path:
but the shared
parse_function_tool_arguments()helper used by the non-streaming and done/final paths does not, which is what led me to believe the empty-arguments case is real and this omission is an oversight rather than intentional.Minimal repro on a clean checkout:
Fix
In both
parse_function_tool_arguments()implementations, normalize empty/whitespace-only arguments to"{}"before parsing:The raw path then returns
{}and thePydanticFunctionToolpath parses"{}"into the model (valid for zero-param/all-optional strict schemas, which are the only ones for which the API emits empty-string arguments). Non-empty arguments are byte-for-byte unaffected, so there is no regression surface. The normalization is scoped strictly to empty/whitespace strings; if the API ever sent""for a schema with required fields, the Pydantic path would now surface a clearerValidationError(missing fields) rather than a JSON-syntax error.Testing
Added offline, plain-assert unit tests (no network, no mock server, no snapshots):
tests/lib/chat/test_completions.py— direct helper (empty / whitespace ->{}, non-empty regression guard,PydanticFunctionToolno-args model -> instance) and an end-to-endparse_chat_completioncase that assertsparsed_arguments == {}.tests/lib/responses/test_responses.py— the responses variant of the helper.tests/lib/chat/test_completions_streaming.py— feeds hand-built chunks throughChatCompletionStreamState; pre-fixhandle_chunkraisedJSONDecodeError, post-fix thetool_calls.function.arguments.doneevent fires withparsed_arguments == {}andget_final_completion()agrees.The nine empty-argument assertions fail on a clean checkout and pass with the fix; the full
tests/lib/suite passes, andruff check/ruff format --check/pyright/mypyare clean on the changed files.Additional context & links
None — this is a self-discovered bug; I could not find an existing issue or PR describing empty-string arguments to
parse_function_tool_arguments.