OpenAI Responses API LLM service implementation #3164
+817
−0
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.
Overview
Complete migration from OpenAI Chat Completions API to the new Responses API with stateful conversation support and incremental mode implementation.
Motivation
The OpenAI Responses API provides several advantages over the Chat Completions API:
store=True)previous_response_id(90% token savings)ResponseStreamEventCore Service:
OpenAIResponsesLLMServiceA new LLM service that inherits from
BaseOpenAILLMServiceand implements the Responses API:Key Features
1. API Endpoint Migration
client.chat.completions.create()client.responses.create()2. Message Format Conversion
Converts Chat Completions message format to Responses API input format:
Chat Completions format:
{"role": "user", "content": "Hello"}Responses API format:
{ "type": "message", "role": "user", "content": [{"type": "input_text", "text": "Hello"}] }Key conversions handled:
"text"→"input_text"(user messages)"text"→"output_text"(assistant messages)"image_url"→"input_image"(with base64 data extraction)call_idat top levelfunction_call_outputitems3. Stateful Conversations (
store=True)Enables server-side conversation storage with incremental updates:
Benefits:
4. Incremental Mode
Implements robust context reset detection using dual-signal detection:
Tracking Dictionaries:
Detection Logic:
5. Event Stream Processing
Handles 7 event types from Responses API:
6. Tool Calling Format
Converts nested Chat Completions tool format to flat Responses API format:
Chat Completions (nested):
{ "type": "function", "function": { "name": "get_weather", "description": "Get weather data", "parameters": {...} } }Responses API (flat):
{ "type": "function", "name": "get_weather", "description": "Get weather data", "parameters": {...} }Context Aggregators
Provides OpenAI-specific context aggregators:
Integration:
bot.pyImport and Configuration
Pipecat Flows Compatibility -- IMPORTANT until pipecat_flows is also updated
Uses monkey-patch for seamless integration:
Latency Improvement
Backward Compatibility
BaseOpenAILLMService- drop-in replacementOpenAILLMContextand universalLLMContextAPI Compatibility
reasoning_effortparameterArchitecture
BaseOpenAILLMServiceError Handling
Logging
Migration Guide
For Existing Users
Before (Chat Completions):
After (Responses API):
Configuration Options