Repository navigation
fix(responses): hoist Codex additional_tools input items into the chat bridge tools #40989
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
Merged
mateo-berri
merged 3 commits into
main
from
litellm_responses_bridge_hoist_additional_tools
Sep 15, 2026
+560
−104
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
e732a48
fix(responses): hoist Codex additional_tools input items into the cha…
mateo-berri 7ea19ec
fix(responses): keep namespace custom tools through guardrail merges …
mateo-berri e3152c0
fix(responses): classify streamed tool calls on the chat name and str…
mateo-berri 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
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
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,65 @@ | ||
| from collections.abc import Sequence | ||
| from dataclasses import dataclass | ||
| from typing import Final, cast # noqa: TID251 # validating the openai tool union strips vendor keys from raw tools | ||
|
|
||
| from pydantic import BaseModel, ValidationError | ||
|
|
||
| from litellm._logging import verbose_logger | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| from litellm.types.llms.openai import ALL_RESPONSES_API_TOOL_PARAMS, ResponseInputParam | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
|
|
||
| ADDITIONAL_TOOLS_INPUT_ITEM_TYPE: Final = "additional_tools" | ||
|
|
||
|
|
||
| class _InputItemType(BaseModel): | ||
| type: str = "" | ||
|
|
||
|
|
||
| class _AdditionalToolsItem(BaseModel): | ||
| tools: tuple[dict[str, object], ...] = () | ||
|
|
||
|
|
||
| @dataclass(frozen=True, slots=True) | ||
| class HoistedAdditionalTools: | ||
| input: str | ResponseInputParam | ||
| tools: tuple[ALL_RESPONSES_API_TOOL_PARAMS, ...] | ||
| hoisted: tuple[ALL_RESPONSES_API_TOOL_PARAMS, ...] | ||
|
|
||
|
|
||
| def _is_additional_tools_item(item: object) -> bool: | ||
| try: | ||
| return _InputItemType.model_validate(item).type == ADDITIONAL_TOOLS_INPUT_ITEM_TYPE | ||
| except ValidationError: | ||
| return False | ||
|
|
||
|
|
||
| def _tools_of_item(item: object) -> tuple[ALL_RESPONSES_API_TOOL_PARAMS, ...]: | ||
| try: | ||
| parsed: Final = _AdditionalToolsItem.model_validate(item) | ||
| except ValidationError: | ||
| return () | ||
| return tuple( | ||
| cast( | ||
| "ALL_RESPONSES_API_TOOL_PARAMS", tool | ||
| ) # cast-ok: nested tools carry the same raw tool JSON as top-level tools | ||
| for tool in parsed.tools | ||
| ) | ||
|
|
||
|
|
||
| def hoist_additional_tools( | ||
| input: str | ResponseInputParam, | ||
| tools: Sequence[ALL_RESPONSES_API_TOOL_PARAMS] | None, | ||
| ) -> HoistedAdditionalTools: | ||
| existing: Final = tuple(tools or ()) | ||
| if isinstance(input, str): | ||
| return HoistedAdditionalTools(input=input, tools=existing, hoisted=()) | ||
| items: Final = tuple(item for item in input if _is_additional_tools_item(item)) | ||
| if not items: | ||
| return HoistedAdditionalTools(input=input, tools=existing, hoisted=()) | ||
| hoisted: Final = tuple(tool for item in items for tool in _tools_of_item(item)) | ||
| verbose_logger.debug( | ||
| "Responses API: hoisting %d tool(s) out of %d 'additional_tools' input item(s) into the top-level tools param.", | ||
| len(hoisted), | ||
| len(items), | ||
| ) | ||
| remaining_input: Final = [item for item in input if not _is_additional_tools_item(item)] | ||
| return HoistedAdditionalTools(input=remaining_input, tools=(*existing, *hoisted), hoisted=hoisted) | ||
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a guardrail rewrite contains the namespace prefix internally,
replaceremoves legitimate text instead of only removing a boundary prefixThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The prefix is the namespace description plus a blank line, re-added on flatten; stripping its first copy stops a prepended guardrail marker doubling it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That clarifies the intent. The prefix is the wrapper introduced during flattening, and it may appear after a guardrail-prepended marker rather than at offset zero. Removing only its first occurrence prevents that wrapper from being duplicated when the tool is flattened again while preserving the marker. The existing regression test covers the guardrail edit case, so I’m withdrawing the concern.