Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 29, 2025

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

  • Added _convert_content_item_to_responses_format() helper function to convert both images and text from Chat API to Responses API format
  • Updated _convert_chat_request_to_responses_request() to use the new helper function
  • Added comprehensive tests for both base64 and URL images
  • Verified async version works correctly
  • All existing tests pass - no regressions
  • Linting checks pass
  • CodeQL security scan - no vulnerabilities found

Technical 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:

  • Text items: type: "text"type: "input_text"
  • Image items: type: "image_url"type: "input_image" with image_url as a direct property

Testing

  • Added test_responses_api_converts_images_correctly() to test conversion logic for both text and images
  • Added test_responses_api_with_image_input() to test complete flow with LM class
  • All existing Responses API tests pass
  • Manually tested sync and async code paths
  • Verified format matches OpenAI documentation

Security Summary

CodeQL security scan completed with no vulnerabilities found.

Fixes #8985

Original prompt

This section details on the original issue you should resolve

<issue_title>[Bug] OpenAI Responses model_type fails with Image inputs</issue_title>
<issue_description>### What happened?

When using the Responses API for OpenAI models, Image inputs aren't handled correctly.

Given the following code:

import python

class ImageDescriptionGeneratorSignature(dspy.Signature):
    image: dspy.Image = dspy.InputField(desc="")
    image_description: str = dspy.OutputField(desc="")

descriptor = dspy.Predict(ImageDescriptionGeneratorSignature)

lm = dspy.LM(
    model="openai/gpt-5-mini",
    api_key="MY-KEY",
    max_tokens=16000,
    temperature=1.0,
    model_type="responses"
)

dspy.configure(lm=lm)
img = dspy.Image("overload.jpg")

result = descriptor(image=img)
print(result.image_description)

This throws a LiteLLM error:

BadRequestError: litellm.BadRequestError: OpenAIException - {
  "error": {
    "message": "Invalid value: 'text'. Supported values are: 'input_text', 'input_image', 'output_text', 'refusal', 'input_file', 'computer_screenshot', and 'summary_text'.",
    "type": "invalid_request_error",
    "param": "input[0].content[1].type",
    "code": "invalid_value"
  }
}

To fix the issue, we need to make sure we're formatting images correctly for the Responses API.

Steps to reproduce

import python

class ImageDescriptionGeneratorSignature(dspy.Signature):
image: dspy.Image = dspy.InputField(desc="")
image_description: str = dspy.OutputField(desc="")

descriptor = dspy.Predict(ImageDescriptionGeneratorSignature)

lm = dspy.LM(
model="openai/gpt-5-mini",
api_key="MY-KEY",
max_tokens=16000,
temperature=1.0,
model_type="responses"
)

dspy.configure(lm=lm)
img = dspy.Image("overload.jpg")

result = descriptor(image=img)
print(result.image_description)

DSPy version

3.0.4b1</issue_description>

<agent_instructions>Can you support the request conversion for image inputs? Refer to https://platform.openai.com/docs/api-reference/responses</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💡 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.

Copilot AI changed the title [WIP] Fix OpenAI Responses API for image inputs Fix image input conversion for OpenAI Responses API Oct 29, 2025
Copilot AI requested a review from TomeHirata October 29, 2025 07:08
Copilot finished work on behalf of TomeHirata October 29, 2025 07:08


def test_responses_api_converts_images_correctly():
"""Test that image_url format is converted to input_image format for Responses API."""
Copy link
Collaborator

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 43c8fc9

Copilot AI requested a review from TomeHirata October 30, 2025 10:13
Copilot finished work on behalf of TomeHirata October 30, 2025 10:13
For images, converts from:
{"type": "image_url", "image_url": {"url": "..."}}
To:
{"type": "input_image", "source": {"type": "url", "url": "..."}}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

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.

Copilot AI requested a review from TomeHirata October 30, 2025 10:22
Copilot finished work on behalf of TomeHirata October 30, 2025 10:22
"image_url": image_url,
}

# For non-image items, return as-is
Copy link
Collaborator

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

Copy link
Contributor Author

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.

Copilot AI changed the title Fix image input conversion for OpenAI Responses API Fix content input conversion for OpenAI Responses API Oct 30, 2025
Copilot finished work on behalf of TomeHirata October 30, 2025 11:17
Copilot AI requested a review from TomeHirata October 30, 2025 11:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] OpenAI Responses model_type fails with Image inputs

2 participants