Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

Fix Responses API structured outputs (#9130)

Olivier7455afa89631
diff --git a/dspy/clients/lm.py b/dspy/clients/lm.py
index f4885d470..9df61ae03 100644
--- a/dspy/clients/lm.py
+++ b/dspy/clients/lm.py
@@ -6,6 +6,7 @@ import warnings
from typing import Any, Literal, cast
import litellm
+import pydantic
from anyio.streams.memory import MemoryObjectSendStream
from asyncer import syncify
@@ -501,6 +502,12 @@ def _convert_chat_request_to_responses_request(request: dict[str, Any]):
# Convert `response_format` to `text.format` for Responses API
if "response_format" in request:
response_format = request.pop("response_format")
+ if isinstance(response_format, type) and issubclass(response_format, pydantic.BaseModel):
+ response_format = {
+ "name": response_format.__name__,
+ "type": "json_schema",
+ "schema": response_format.model_json_schema(),
+ }
text = request.pop("text", {})
request["text"] = {**text, "format": response_format}
diff --git a/tests/adapters/test_json_adapter.py b/tests/adapters/test_json_adapter.py
index 373c01c67..f376831d9 100644
--- a/tests/adapters/test_json_adapter.py
+++ b/tests/adapters/test_json_adapter.py
@@ -978,8 +978,11 @@ def test_json_adapter_with_responses_api():
call_kwargs = mock_responses.call_args.kwargs
assert "response_format" not in call_kwargs
assert "text" in call_kwargs
- assert isinstance(call_kwargs["text"]["format"], type)
- assert issubclass(call_kwargs["text"]["format"], pydantic.BaseModel)
+
+ assert isinstance(call_kwargs["text"]["format"], dict)
+ assert isinstance(call_kwargs["text"]["format"]["name"], str)
+ assert call_kwargs["text"]["format"]["type"] == "json_schema"
+ assert isinstance(call_kwargs["text"]["format"]["schema"], dict)
def test_format_system_message():
diff --git a/tests/clients/test_lm.py b/tests/clients/test_lm.py
index 65bf63496..01d6b90e9 100644
--- a/tests/clients/test_lm.py
+++ b/tests/clients/test_lm.py
@@ -931,3 +931,57 @@ def test_responses_api_with_image_input():
image_content = [c for c in content if c.get("type") == "input_image"]
assert len(image_content) == 1
assert image_content[0]["image_url"] == "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
+
+
+def test_responses_api_with_pydantic_model_input():
+ api_response = make_response(
+ output_blocks=[
+ ResponseOutputMessage(
+ **{
+ "id": "msg_1",
+ "type": "message",
+ "role": "assistant",
+ "status": "completed",
+ "content": [
+ {
+ "type": "output_text",
+ "text": '{"answer" : "This is a good test answer", "number" : 42}',
+ "annotations": [],
+ }
+ ],
+ },
+ ),
+ ]
+ )
+
+ lm = dspy.LM(
+ model="openai/gpt-5-mini",
+ model_type="responses",
+ cache=False,
+ temperature=1.0,
+ max_tokens=16000,
+ )
+
+ class TestModel(pydantic.BaseModel):
+ answer: str
+ number: int
+
+ with mock.patch("litellm.responses", autospec=True, return_value=api_response) as dspy_responses:
+ # Test with messages containing a Pydantic model as response format
+ lm_result = lm("What is a good test answer?", response_format=TestModel)
+
+ # Try to validate to Pydantic model
+ TestModel.model_validate_json(lm_result[0]["text"])
+
+ dspy_responses.assert_called_once()
+ call_args = dspy_responses.call_args.kwargs
+
+ # Verify the request was converted correctly
+ assert "text" in call_args
+ response_format = call_args["text"]["format"]
+
+ assert response_format == {
+ "name": TestModel.__name__,
+ "type": "json_schema",
+ "schema": TestModel.model_json_schema(),
+ }