Skip to content

fix: accept dict tool arguments in ChatMessage.from_openai_dict_format - #12790

Merged
sjrl merged 3 commits into
deepset-ai:mainfrom
MohammadHijjawi97:fix/openai-dict-tool-arguments
Sep 28, 2026
Merged

sjrl merged 3 commits into
deepset-ai:mainfrom
MohammadHijjawi97:fix/openai-dict-tool-arguments

Conversation

@MohammadHijjawi97

Copy link
Copy Markdown
Contributor

Related Issues

No existing issue. This PR includes a regression test for the crash.

Proposed Changes:

Proposed Changes:

ChatMessage.from_openai_dict_format always passed tool-call arguments to json.loads. Some OpenAI-compatible servers already send a parsed dict, which raised TypeError. Invalid JSON strings also crashed. Accept a dict payload, treat empty/null/invalid JSON as {}.

How did you test it?

Added unit tests for dict arguments and invalid JSON. Existing empty/missing argument tests still pass.

Notes for the reviewer

Checklist

  • I have read the contributors guidelines and the code of conduct.
  • I have updated the related issue with new insights and changes.
  • I have added unit tests and updated the docstrings.
  • I've used one of the conventional commit types for my PR title: fix:, feat:, build:, chore:, ci:, docs:, style:, refactor:, perf:, test: and added ! in case the PR includes breaking changes.
  • I have documented my code.
  • I have added a release note file, following the contributors guidelines.
  • I have run pre-commit hooks and fixed any issue.

Some OpenAI-compatible servers send parsed argument objects. json.loads then raised TypeError. Invalid JSON strings are treated as an empty mapping.
@MohammadHijjawi97
MohammadHijjawi97 requested a review from a team as a code owner September 17, 2026 11:05
@MohammadHijjawi97
MohammadHijjawi97 requested review from sjrl and removed request for a team September 17, 2026 11:05
@vercel

vercel Bot commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

@MohammadHijjawi97 is attempting to deploy a commit to the deepset Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions

Copy link
Copy Markdown
Contributor

Hi @MohammadHijjawi97, thanks for your interest in contributing to Haystack! 🙏

⚠️ You currently have 5 open pull requests in this repository (#12794, #12793, #12792, #12791 and this one). Our review capacity is limited, so please hold off opening more PRs until we've had a chance to review your first 2 open PRs. This helps us give each contribution the attention it deserves. Thank you!

This is an automated message to help us keep the review queue healthy.

OpenAI sends a JSON string. Some OpenAI-compatible servers already send a dict, omit the field, or send
null or an empty string for a zero-argument call.
"""
if isinstance(raw_arguments, dict):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from_openai_dict_format is meant to mirror to_openai_dict_format, which always emits arguments as a JSON string. Which OpenAI-compatible server sends a parsed dict here? Our chat generators don't route responses through this method.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point. I hit this by calling from_openai_dict_format directly with arguments already parsed as a dict (some OpenAI-compatible SDKs / providers return tool call args that way). Haystack's own chat generators do not go through this method, so I do not have a production Haystack path that needs it. Happy to close this if you would rather keep the contract mirrored to to_openai_dict_format only.

@MohammadHijjawi97

Copy link
Copy Markdown
Contributor Author

CLA is signed now. This is ready for review whenever you have time.

@MohammadHijjawi97
MohammadHijjawi97 force-pushed the fix/openai-dict-tool-arguments branch from d9c2a04 to d1b33aa Compare September 25, 2026 08:30
@MohammadHijjawi97

Copy link
Copy Markdown
Contributor Author

Fair question. I don't have a concrete server that sends a parsed dict here; this was a defensive change rather than a fix for a reported failure. Since from_openai_dict_format is meant to mirror to_openai_dict_format and your chat generators don't route responses through it, I don't think the dict branch is justified. I'm happy to close this PR to free up review capacity, unless you'd like to keep just the invalid-JSON handling.

@sjrl

sjrl commented Sep 28, 2026

Copy link
Copy Markdown
Contributor

Thanks for the PR @MohammadHijjawi97! Accepting dict arguments makes sense. I'd just like to keep raising on invalid JSON like before, so a truncated tool call like "{\"path\": \"/data/rep" doesn't silently become a valid zero-argument call. Since the change is small I'll push it to this branch directly.

@github-actions

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/dataclasses
  chat_message.py
Project Total  

This report was generated by python-coverage-comment-action

@sjrl sjrl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@sjrl
sjrl merged commit 54f7e25 into deepset-ai:main Sep 28, 2026
23 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic:tests type:documentation Improvements on the docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants