- 
                Notifications
    You must be signed in to change notification settings 
- Fork 2.4k
Fix content input conversion for OpenAI Responses API #8993
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
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: TomeHirata <[email protected]>
Co-authored-by: TomeHirata <[email protected]>
Co-authored-by: TomeHirata <[email protected]>
        
          
                tests/clients/test_lm.py
              
                Outdated
          
        
      |  | ||
|  | ||
| def test_responses_api_converts_images_correctly(): | ||
| """Test that image_url format is converted to input_image format for Responses API.""" | 
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.
@copilot can we remove docstring for the test cases?
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.
Done in 43c8fc9
Co-authored-by: TomeHirata <[email protected]>
        
          
                dspy/clients/lm.py
              
                Outdated
          
        
      | For images, converts from: | ||
| {"type": "image_url", "image_url": {"url": "..."}} | ||
| To: | ||
| {"type": "input_image", "source": {"type": "url", "url": "..."}} | 
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.
@copilot Does this match the documented interface?
https://platform.openai.com/docs/api-reference/responses/create?lang=curl

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.
You're absolutely right - I was using the wrong format. Fixed in 6da81b3 to match the OpenAI documentation. The Responses API expects {"type": "input_image", "image_url": "..."} with image_url as a direct property, not a nested source object.
Co-authored-by: TomeHirata <[email protected]>
        
          
                dspy/clients/lm.py
              
                Outdated
          
        
      | "image_url": image_url, | ||
| } | ||
|  | ||
| # For non-image items, return as-is | 
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.
@copilot I this we also need to convert text item from type: text to type: input_text. See https://platform.openai.com/docs/api-reference/responses
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.
Good catch! Fixed in 0055def - now converting text items from type: "text" to type: "input_text" to match the OpenAI Responses API format.
Co-authored-by: TomeHirata <[email protected]>
Summary
Fixed a bug where the OpenAI Responses API fails when processing Image and text inputs. The issue was that content items were not being converted from the Chat API format to the Responses API format.
Changes Made
_convert_content_item_to_responses_format()helper function to convert both images and text from Chat API to Responses API format_convert_chat_request_to_responses_request()to use the new helper functionTechnical Details
The Chat API uses these formats:
{"type": "text", "text": "..."} {"type": "image_url", "image_url": {"url": "data:image/png;base64,..."}}The Responses API expects these formats per OpenAI documentation:
{ "type": "input_text", "text": "..." } { "type": "input_image", "image_url": "data:image/png;base64,..." }The fix converts both text and image content items to the correct Responses API format:
type: "text"→type: "input_text"type: "image_url"→type: "input_image"withimage_urlas a direct propertyTesting
test_responses_api_converts_images_correctly()to test conversion logic for both text and imagestest_responses_api_with_image_input()to test complete flow with LM classSecurity Summary
CodeQL security scan completed with no vulnerabilities found.
Fixes #8985
Original prompt
model_typefails withImageinputs #8985💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.