[router] Function call support for openai router Responses API#12386
[router] Function call support for openai router Responses API#12386slin1237 merged 4 commits intosgl-project:mainfrom
Conversation
Summary of ChangesHello @key4ng, 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 focuses on a significant refactoring and enhancement of how function calls and their outputs are handled within the SGLang router. It standardizes naming conventions, updates API response structures to better represent function call details, and ensures that function calls and their results are correctly integrated into the conversation history for persistent storage and retrieval. These changes are crucial for robust and consistent function calling capabilities. Highlights
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 primarily refactors function_tool_call to function_call and introduces a call_id field, aligning the implementation more closely with function calling API conventions. The changes are consistently applied across the codebase and include improvements to conversation persistence logic. I've provided a few suggestions to enhance code maintainability by refactoring repetitive code, and to improve robustness by adding error logging for deserialization failures. I also noted a minor inconsistency in struct definitions that could be addressed for better API consistency.
function_calll_state_test.py``` """ Function Calling Tests with State Management Tests previous_response_id and conversation features """from openai import OpenAI client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"), base_url="http://localhost:30000/v1") Define toolstools = [ Mock functiondef get_current_weather(location, unit="fahrenheit"): print("=" * 80) First requestprint("\n1. Initial request:") print(f"Response ID: {response1.id}") Check for function callsfunction_call_items = [item for item in response1.output if item.type == "function_call"] if function_call_items: print("\n" + "=" * 80) First, create a conversation using the Conversations APIprint("\n1. Creating a new conversation via Conversations API:") print(f"Conversation created: {conversation.id}") Now use this conversation ID for responsesprint(f"\n2. First request in conversation {conversation.id}:") print(f"Response ID: {conversation_response.id}") Check for function callsfunction_call_items = [item for item in conversation_response.output if item.type == "function_call"] if function_call_items: Retrieve the conversation to verify it was storedprint(f"\n5. Retrieving conversation {conversation.id}:") print("\n" + "=" * 80) First requestprint("\n1. Initial weather request:") print(f"Response ID: {response1.id}") Get function callsfunction_call_items = [item for item in response1.output if item.type == "function_call"] if function_call_items: print("\n" + "=" * 80) print("\n1. Creating a new conversation:") print("\n2. First turn in conversation:") print(f"Turn 1 - Response ID: {response1.id}") Execute function if calledfunction_call_items = [item for item in response1.output if item.type == "function_call"] Try to retrieve conversationprint(f"\n3. Retrieving conversation: {test_conversation.id}") print("\n" + "=" * 80) |
|
make sure to rebase |
|
router related test succeeded: https://github.com/sgl-project/sglang/actions/runs/18952340733/job/54120039707?pr=12386 |
Motivation
Implement proper function calling support in the sgl-router Responses API, including state persistence and conversation management for function calls and their outputs.
Modifications
Modifications
- Updated item type in protocols, handlers, and ID generation (sgl-router/src/data_connector/core.rs:271)
- Added call_id field to FunctionToolCall items (in addition to id)
- Added optional id field to FunctionCallOutput
- Flattened function fields in ResponseTool at top level (sgl-router/src/protocols/responses.rs:26)
- Function calls/outputs now stored with full item structure in DB content field
- Proper ID preservation when linking items to conversations
- Better ID prefix generation (fc for function_call items)
- Now loads function_call and function_call_output items from DB (previously only loaded messages)
- Filters out reasoning items (internal processing details) from upstream requests
Accuracy Tests
Benchmarking and Profiling
Checklist