-
-
Notifications
You must be signed in to change notification settings - Fork 11.6k
fix(responses-bridge): improve OpenAI Codex CLI compatibility with Chat Completions providers #31571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
duanhongyi
wants to merge
5
commits into
BerriAI:litellm_internal_staging
from
duanhongyi:fix/codex-compatibility
Closed
fix(responses-bridge): improve OpenAI Codex CLI compatibility with Chat Completions providers #31571
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
49d655d
Merge pull request #31384 from BerriAI/litellm_internal_staging
yuneng-berri a311f11
Merge pull request #31477 from BerriAI/litellm_internal_staging
yuneng-berri 0ade44f
Merge pull request #31542 from BerriAI/litellm_internal_staging
shivamrawat1 88e03e5
Merge pull request #31765 from BerriAI/litellm_internal_staging
yuneng-berri d3ce123
fix(responses-bridge): custom tool round-trip and allowlist preservat…
duanhongyi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
156 changes: 156 additions & 0 deletions
156
litellm/responses/litellm_completion_transformation/custom_tools.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| """ | ||
| Utilities for handling OpenAI Responses API 'custom' tools (freeform/grammar tools) | ||
| when bridging to Chat Completions providers. | ||
|
|
||
| Custom tools are defined with ``type: "custom"`` and a grammar/format specification. | ||
| Since most Chat Completions providers only support standard ``function`` tools, | ||
| the bridge converts them to ``function`` tools with a single ``content`` string | ||
| parameter. When the model responds with a ``function_call`` for such a tool, this | ||
| module converts it back to the ``custom_tool_call`` format expected by clients like | ||
| Codex CLI. | ||
|
|
||
| The forward direction (custom -> function) and reverse direction (function_call -> | ||
| custom_tool_call) are both handled here so future custom tool types can be added by | ||
| extending this module without touching the streaming iterator or transformation | ||
| logic. | ||
| """ | ||
|
|
||
| import json | ||
| from typing import Any | ||
|
|
||
| _MAX_ARGUMENTS_LEN = 1_000_000 | ||
|
|
||
|
|
||
| def extract_custom_tool_names(tools: list[Any] | None) -> set[str]: | ||
| """Extract names of tools originally defined as ``type: "custom"``.""" | ||
| if not tools: | ||
| return set() | ||
| names: set[str] = set() | ||
| for tool in tools: | ||
| if isinstance(tool, dict) and tool.get("type") == "custom" and "name" in tool: | ||
| names.add(tool["name"]) | ||
| return names | ||
|
|
||
|
|
||
| def is_custom_tool_call(tool_name: str, custom_tool_names: set[str]) -> bool: | ||
| """Check if a tool call name corresponds to a custom tool.""" | ||
| return tool_name in custom_tool_names | ||
|
|
||
|
|
||
| def unwrap_custom_tool_arguments(arguments: str) -> str: | ||
| """Extract the raw content string from JSON-wrapped arguments. | ||
|
|
||
| The bridge converts custom tools to function tools with schema | ||
| ``{"properties": {"content": {"type": "string"}}}``, so the model returns | ||
| arguments like ``{"content": "*** Begin Patch\\n..."}``. This function | ||
| extracts just the content string. If the arguments are not valid JSON or do | ||
| not contain a ``content`` key, the original string is returned unchanged. | ||
| """ | ||
| if not arguments: | ||
| return "" | ||
| if len(arguments) > _MAX_ARGUMENTS_LEN: | ||
| return arguments | ||
| try: | ||
| parsed = json.loads(arguments) | ||
| if isinstance(parsed, dict) and "content" in parsed: | ||
| return str(parsed["content"]) | ||
| except (json.JSONDecodeError, TypeError, ValueError): | ||
| pass | ||
| return arguments | ||
|
|
||
|
|
||
| def build_tool_call_item_kwargs( | ||
| call_id: str, | ||
| name: str, | ||
| arguments_or_input: str, | ||
| status: str, | ||
| custom_tool_names: set[str], | ||
| ) -> dict[str, Any]: | ||
| """Build kwargs for an output item dict that is either a ``function_call`` | ||
| or a ``custom_tool_call`` depending on whether *name* is in | ||
| *custom_tool_names*. | ||
|
|
||
| For custom tools the ``arguments`` JSON is unwrapped into the ``input`` | ||
| field. For regular function tools the raw ``arguments`` string is kept. | ||
|
|
||
| This centralises the branching logic so the streaming iterator and the | ||
| non-streaming transformation share a single code path. | ||
| """ | ||
| custom = is_custom_tool_call(name, custom_tool_names) | ||
| item_type = "custom_tool_call" if custom else "function_call" | ||
| kwargs: dict[str, Any] = { | ||
| "type": item_type, | ||
| "id": call_id, | ||
| "call_id": call_id, | ||
| "name": name, | ||
| "status": status, | ||
| } | ||
| if custom: | ||
| if status == "completed": | ||
| kwargs["input"] = unwrap_custom_tool_arguments(arguments_or_input) | ||
| else: | ||
| kwargs["input"] = "" | ||
| else: | ||
| kwargs["arguments"] = arguments_or_input | ||
| return kwargs | ||
|
|
||
|
|
||
| def build_custom_tool_call_item( | ||
| call_id: str, | ||
| name: str, | ||
| input_str: str, | ||
| status: str = "completed", | ||
| ) -> dict[str, Any]: | ||
| """Build a standalone ``custom_tool_call`` output item dict.""" | ||
| if not call_id: | ||
| raise ValueError("call_id is required for custom_tool_call output item") | ||
| return { | ||
| "type": "custom_tool_call", | ||
| "call_id": call_id, | ||
| "id": call_id, | ||
| "name": name, | ||
| "input": input_str, | ||
| "status": status, | ||
| } | ||
|
|
||
|
|
||
| def convert_custom_tool_to_function_tool(tool: dict[str, Any]) -> dict[str, Any] | None: | ||
| """Convert a Responses API ``custom`` tool to a Chat Completions ``function`` | ||
| tool. | ||
|
|
||
| The grammar definition is embedded in the description so the model can | ||
| produce correctly-formatted output. Returns ``None`` if the tool is not a | ||
| valid custom tool. | ||
| """ | ||
| if tool.get("type") != "custom": | ||
| return None | ||
| name = tool.get("name", "") | ||
| desc = tool.get("description", "") | ||
| fmt = tool.get("format", {}) | ||
| if isinstance(fmt, dict) and fmt.get("definition"): | ||
| syntax = fmt.get("syntax", "") | ||
| definition = fmt.get("definition", "") | ||
| desc = desc + "\n\nFormat:\n```" + syntax + "\n" + definition + "\n```" | ||
| allowed_callers = tool.get("allowed_callers") | ||
| if allowed_callers is not None and not ( | ||
| isinstance(allowed_callers, list) and all(isinstance(item, str) for item in allowed_callers) | ||
| ): | ||
| raise ValueError("allowed_callers must be a list of strings") | ||
| return { | ||
| "type": "function", | ||
| "function": { | ||
| "name": name, | ||
| "description": desc, | ||
| "parameters": { | ||
| "type": "object", | ||
| "properties": { | ||
| "content": { | ||
| "type": "string", | ||
| "description": f"The {name} content following the specified format", | ||
| } | ||
| }, | ||
| "required": ["content"], | ||
| }, | ||
| }, | ||
| **({"allowed_callers": allowed_callers} if allowed_callers is not None else {}), | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.