fix: handle empty or missing tool-call arguments in from_openai_dict_format#11972
fix: handle empty or missing tool-call arguments in from_openai_dict_format#11972chuenchen309 wants to merge 5 commits into
Conversation
|
@chuenchen309 is attempting to deploy a commit to the deepset Team on Vercel. A member of the Team first needs to authorize it. |
bogdankostic
left a comment
There was a problem hiding this comment.
Thanks for the PR @chuenchen309. It already looks good in principle, we just need to update the release notes.
There was a problem hiding this comment.
This release note doesn't seem to be related to the changes made in this PR.
| Fixed `ChatMessage.from_openai_dict_format` crashing on zero-argument tool | ||
| calls. OpenAI-compatible servers (vLLM, llama.cpp, Ollama, LM Studio, ...) may | ||
| send an empty string or omit the `arguments` field for a tool call that takes | ||
| no arguments; this previously raised `json.JSONDecodeError` or `KeyError`. | ||
| Such tool calls are now parsed into a `ToolCall` with empty `arguments`. |
There was a problem hiding this comment.
We need to use double-backticks for in-line code in release notes.
| Fixed `ChatMessage.from_openai_dict_format` crashing on zero-argument tool | |
| calls. OpenAI-compatible servers (vLLM, llama.cpp, Ollama, LM Studio, ...) may | |
| send an empty string or omit the `arguments` field for a tool call that takes | |
| no arguments; this previously raised `json.JSONDecodeError` or `KeyError`. | |
| Such tool calls are now parsed into a `ToolCall` with empty `arguments`. | |
| Fixed ``ChatMessage.from_openai_dict_format`` crashing on zero-argument tool | |
| calls. OpenAI-compatible servers (vLLM, llama.cpp, Ollama, LM Studio, ...) may | |
| send an empty string or omit the ``arguments`` field for a tool call that takes | |
| no arguments; this previously raised ``json.JSONDecodeError`` or ``KeyError``. | |
| Such tool calls are now parsed into a ``ToolCall`` with empty ``arguments``. |
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
…format `ChatMessage.from_openai_dict_format` did `json.loads(tc["function"]["arguments"])`, assuming `arguments` is always present and a valid JSON string. Zero-argument tool calls from OpenAI-compatible servers (vLLM, llama.cpp, Ollama, LM Studio, ...) send an empty string, null, or omit the key entirely, which raised `json.JSONDecodeError` or `KeyError` — even though the method docstring courts these 'shallow OpenAI-compatible APIs'. The message validator only checks that a `function` field exists, so nothing guarded this. Treat empty/null/missing arguments as an empty dict, leaving valid-JSON parsing unchanged. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…scope for this PR)
8dc2559 to
28d7ca8
Compare
|
Thanks @bogdankostic — added the release note for the fix and rebased onto latest |
Problem
ChatMessage.from_openai_dict_formatparses tool-call arguments with:This assumes
argumentsis always present and always a valid JSON string. Azero-argument tool call from an OpenAI-compatible server (vLLM, llama.cpp,
Ollama, LM Studio, ...) commonly sends an empty string,
null, or omits the key:_validate_openai_messageonly checks that afunctionfield exists, so nothingguards this — and the method's docstring explicitly courts these "shallow
OpenAI-compatible APIs".
Fix
Treat empty/null/missing
argumentsas an empty dict, leaving valid-JSON parsingunchanged:
Tests
Added two tests in
test/dataclasses/test_chat_message.py(empty-string andmissing-key arguments). Both fail before the change (
JSONDecodeError/KeyError) and pass after. All 15from_openai_dict_formattests pass.ruff check/ruff format --checkpass. Added a release note.Disclosure: I used AI assistance (Claude) to find this crash-on-edge-input and
draft the tests. I reviewed the change, ran the suite, and take responsibility
for its correctness.