Fix Responses API tool type constraint to support function tools.#16806
Fix Responses API tool type constraint to support function tools.#16806Leoyzen wants to merge 10 commits intosgl-project:mainfrom
Conversation
Implement ResponseTool union type to support both built-in tools (web_search_preview, code_interpreter) and function tools, fixing the pydantic validation error that prevented using custom function calls via the standard OpenAI Responses API format. Changes: - Add ResponseBuiltinTool and ResponseFunctionTool classes - Use Union type with discriminator for ResponseTool - Update get_developer_message to accept both Tool and ResponseTool types - Add comprehensive test cases with register_cpu_ci marker - Move test to test/registered/openai_server/basic/ to follow project conventions - Use 'default' suite similar to other protocol validation tests Fixes: Response API tool type constraint bug Documentation: docs/rfc/rfc_response_tool_fix.md
Summary of ChangesHello @Leoyzen, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a critical type constraint issue within SGLang's Responses API, which previously prevented the use of custom function tools. By adopting a more flexible Pydantic model using Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request is a great improvement, fixing a key limitation in the Responses API by allowing type="function" for tools. The use of a discriminated union in protocol.py is well-implemented, and the new tests provide good coverage for the Pydantic models.
However, I've found a critical issue in harmony_utils.py where the code doesn't correctly handle the two different structures for function tools (openai.Tool vs. ResponseFunctionTool), which would lead to a runtime error. My review includes a code suggestion to fix this.
|
I'm very interested in this PR 👍 |
|
/tag-and-rerun-ci |
|
Thank you very much for your contribution. I have found some issues while using it
|
feat: add response request tool render in chat_template and restructure output
|
@JustinTong0323 Please take a look at this pr. |
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request effectively resolves the type constraint issue in the ResponseTool class, enabling the use of custom function tools in the Responses API. The introduction of ResponseBuiltinTool and ResponseFunctionTool with a discriminated union is a clean and robust solution. The accompanying tests are thorough and cover the new functionality well. My review includes a suggestion to refactor some duplicated code for better maintainability and a fix for a minor issue in one of the new tests.
test/registered/openai_server/basic/test_response_tool_parsing.py
Outdated
Show resolved
Hide resolved
|
This fix worked for me, thanks! |
|
Is this issue fixed in the latest build? May I know? |
…er method - Add private method _convert_response_tools_to_tools() in OpenAIServingResponses - Replace duplicate conversion logic in _make_request() and _make_response_output_items() - Fix AttributeError in test_response_tool_parsing.py by removing invalid del statement
ResponseTool only accepted 'web_search_preview' and 'code_interpreter'. Add ResponseFunctionTool to support type='function' with name/desc/params, and make ResponseTool a Union of both.
Motivation
This PR fixes the type constraint issue in SGLang's Response API
ResponseToolclass that prevented users from using custom function calls via the standard OpenAI Responses API format.Previously, the
ResponseToolclass only acceptedtype="web_search_preview"ortype="code_interpreter", causing pydantic validation errors when users tried to provide custom function tools withtype="function". This severely limited the usability of SGLang's Responses API, as it didn't match OpenAI's actual API specification which supports both built-in tools and custom function tools.Example error users encountered:
This fix using a Union type with discriminator to support both built-in and function tools while maintaining type safety.
Related issue: #10038
Modifications
Core Changes
Updated
/python/sglang/srt/entrypoints/openai/protocol.pyResponseBuiltinToolclass to handle built-in tools (web_search_preview,code_interpreter)ResponseFunctionToolclass to handle custom function tools withname,description,parameters, andstrictfieldsResponseToolclass with a Union type alias using discriminator for safe type handlingtyping_extensions.Annotatedfor Union type annotationUpdated
/python/sglang/srt/entrypoints/harmony_utils.pyget_developer_message()function to accept bothlist[Tool](OpenAI type) andlist["ResponseTool"](SGLang type)TYPE_CHECKINGimport to avoid circular import issuesname,description, andparametersattributesTest Changes
/test/registered/openai_server/basic/test_response_tool_fix.pytest_response_tool_function_type_rejected(): Validates function tool acceptancetest_builtin_tools_work(): Ensures backward compatibility with built-in toolstest_mixed_tools(): Tests mixing built-in and function toolstest_empty_tools(): Verifies empty tool lists work correctlytest_function_tool_with_full_schema(): Tests complete function tool schema with nested parametersregister_cpu_ci(est_time=2, suite="default")marker for CI integrationtest/registered/openai_server/basic/to follow project conventionsAccuracy Tests
N/A - This change only affects request validation and data models. It does not modify model inference, kernel logic, or output generation logic. The existing test suite verifies correct parsing and validation behavior.
Benchmarking and Profiling
N/A - No performance impact. The change uses pydantic's built-in Union type handling with discriminator, which has comparable performance to the previous Literal type constraint. Execution time of
ResponsesRequestvalidation remains ~1.6 seconds as verified by test suite.Checklist
Review Process
/tag-run-ci-label,/rerun-failed-ci,/tag-and-rerun-ci