fix: JSON-serialize ToolCallBlock.tool_kwargs in to_openai_message_dict#21389
Open
NIK-TIGER-BILL wants to merge 1 commit intorun-llama:mainfrom
Open
fix: JSON-serialize ToolCallBlock.tool_kwargs in to_openai_message_dict#21389NIK-TIGER-BILL wants to merge 1 commit intorun-llama:mainfrom
NIK-TIGER-BILL wants to merge 1 commit intorun-llama:mainfrom
Conversation
The OpenAI Chat Completions API expects 'function.arguments' to be a JSON string, but ToolCallBlock.tool_kwargs can be a dict. This caused 400 BadRequestError when using mixed LLM providers (e.g., Anthropic orchestrator handing off to an OpenAI sub-agent). Changes: - Serialize dict tool_kwargs to JSON string in to_openai_message_dict - Add tests for both dict and string tool_kwargs scenarios Fixes run-llama#21378 Signed-off-by: NIK-TIGER-BILL <nik.tiger.bill@github.com>
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.
Fixes #21378
Problem
When using AgentWorkflow with mixed LLM providers (e.g., Anthropic orchestrator handing off to an OpenAI sub-agent), the OpenAI Chat Completions API returns a 400 BadRequestError because
function.argumentswas sent as a JSON object instead of a JSON string.Root Cause
In
llama_index/llms/openai/utils.py, theto_openai_message_dictfunction placedblock.tool_kwargsdirectly into "arguments" without JSON serialization. The OpenAI Chat Completions API expectsfunction.argumentsto be a JSON string.Fix
tool_kwargsto JSON string before assigning tofunction.argumentstool_kwargsare passed through unchanged (backward compatible)Changes
to_openai_message_dict()inllama_index/llms/openai/utils.pytests/test_openai_utils.py:test_chat_completions_tool_kwargs_serialized_to_json_string()- verifies dict serializationtest_chat_completions_tool_kwargs_string_passthrough()- verifies string passthroughTesting
cd llama-index-integrations/llms/llama-index-llms-openai pytest tests/test_openai_utils.py::test_chat_completions_tool_kwargs_serialized_to_json_string -v pytest tests/test_openai_utils.py::test_chat_completions_tool_kwargs_string_passthrough -v