DSPy: The framework for programming—not prompting—language models
Python
fix(lm): send Responses-native tool and tool_choice shapes (#10026)
Isaac Miller0312f0da6005
diff --git a/dspy/adapters/base.py b/dspy/adapters/base.pyindex d69ae43ee..3e2ffa599 100644--- a/dspy/adapters/base.py+++ b/dspy/adapters/base.py@@ -157,7 +157,7 @@ class Adapter:text = outputif isinstance(output, dict):- text = output["text"]+ text = output.get("text")output_logprobs = output.get("logprobs")tool_calls = output.get("tool_calls")@@ -733,7 +733,9 @@ def _provider_value(value: Any, key: str, default: Any = None) -> Any:def _provider_tool_call_to_tool_call_dict(tool_call: Any) -> dict[str, Any]:function = _provider_value(tool_call, "function", {}) or {}- arguments = _provider_value(function, "arguments", {})+ # Responses API tool calls carry `arguments` at the top level rather than+ # under `function`.+ arguments = _provider_value(function, "arguments", None) or _provider_value(tool_call, "arguments", {})if isinstance(arguments, str):parsed_arguments = json_repair.loads(arguments)elif isinstance(arguments, dict):@@ -742,7 +744,9 @@ def _provider_tool_call_to_tool_call_dict(tool_call: Any) -> dict[str, Any]:parsed_arguments = {}return {- "id": _provider_value(tool_call, "id") or _provider_value(tool_call, "call_id"),+ # Responses API items carry both an item `id` ("fc_...") and the+ # `call_id` that tool results must reference; prefer `call_id`.+ "id": _provider_value(tool_call, "call_id") or _provider_value(tool_call, "id"),"name": _provider_value(function, "name") or _provider_value(tool_call, "name"),"args": parsed_arguments,}diff --git a/dspy/clients/cache.py b/dspy/clients/cache.pyindex 28d350d18..1db6bcc3e 100644--- a/dspy/clients/cache.py+++ b/dspy/clients/cache.py@@ -151,7 +151,10 @@ class Cache:if hasattr(response, "usage"):# Clear the usage data when cache is hit, because no LM call is maderesponse.usage = {}- response.cache_hit = True+ # Strict provider models (e.g. litellm's ResponsesAPIResponse) reject+ # undeclared attributes in pydantic __setattr__, so stamp the marker+ # directly on the instance dict.+ object.__setattr__(response, "cache_hit", True)return responsedef put(diff --git a/dspy/clients/lm.py b/dspy/clients/lm.pyindex 468b4dc0b..b6fa8ffb4 100644--- a/dspy/clients/lm.py+++ b/dspy/clients/lm.py@@ -7,15 +7,16 @@ import warningsfrom typing import Any, Literal, castimport anyio.from_thread-import pydanticfrom anyio.streams.memory import MemoryObjectSendStreamimport dspyfrom dspy.clients._litellm import get_litellm, is_litellm_context_window_errorfrom dspy.clients.cache import request_cachefrom dspy.clients.openai import OpenAIProvider+from dspy.clients.openai_format import to_openai_responses_requestfrom dspy.clients.provider import Provider, ReinforceJob, TrainingJobfrom dspy.clients.utils_finetune import TrainDataFormat+from dspy.core.types import LMRequestfrom dspy.utils.callback import BaseCallbackfrom dspy.utils.exceptions import (ContextWindowExceededError,@@ -618,83 +619,92 @@ async def alitellm_responses_completion(request: dict[str, Any], num_retries: indef _convert_chat_request_to_responses_request(request: dict[str, Any]):- """- Convert a chat request to a responses request- See https://platform.openai.com/docs/api-reference/responses/create for the responses API specification.- Also see https://platform.openai.com/docs/api-reference/chat/create for the chat API specification.+ """Convert legacy chat-shaped LM kwargs into Responses API kwargs.++ This is the legacy door into the normalized Responses mapper. Inputs the+ old pass-through converter forwarded verbatim — Responses-native shapes and+ provider-SDK dumps — are tolerated here and only here; the typed path stays+ strict."""request = dict(request)- if "messages" in request:- input_items = []- for msg in request.pop("messages"):- content_blocks = []- c = msg.get("content")- if isinstance(c, str):- content_blocks.append({"type": "input_text", "text": c})- elif isinstance(c, list):- # Convert each content item from Chat API format to Responses API format- for item in c:- content_blocks.append(_convert_content_item_to_responses_format(item))- input_items.append({"role": msg.get("role", "user"), "content": content_blocks})- request["input"] = input_items- # Convert `reasoning_effort` to reasoning format supported by the Responses API+ model = request.pop("model")+ messages = [_sanitize_legacy_message(message) for message in request.pop("messages", [])]+ tools = list(request.pop("tools", None) or [])++ # Reasoning models use `max_completion_tokens` in the chat path. The+ # normalized Responses mapper expects the shared `max_tokens` name and emits+ # `max_output_tokens`.+ if "max_completion_tokens" in request and "max_tokens" not in request:+ request["max_tokens"] = request.pop("max_completion_tokens")++ # Preserve the legacy `reasoning_effort=...` Responses behavior from this LM+ # compatibility shim: requesting reasoning effort also asks OpenAI for an+ # automatic reasoning summary, which DSPy can expose as `reasoning_content`.+ # An explicit per-call `reasoning` dict wins verbatim; the effort shorthand+ # is discarded rather than merged.if "reasoning_effort" in request:effort = request.pop("reasoning_effort")- request["reasoning"] = {"effort": effort, "summary": "auto"}-- # 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}-- return request---def _convert_content_item_to_responses_format(item: dict[str, Any]) -> dict[str, Any]:- """- Convert a content item from Chat API format to Responses API format.-- For images, converts from:- {"type": "image_url", "image_url": {"url": "..."}}- To:- {"type": "input_image", "image_url": "..."}-- For text, converts from:- {"type": "text", "text": "..."}- To:- {"type": "input_text", "text": "..."}-- For other types, passes through as-is.+ if request.get("reasoning") is None:+ request["reasoning"] = {"effort": effort, "summary": "auto"}++ # Hosted Responses tools (web_search, file_search, code_interpreter, mcp, ...)+ # have no normalized LMToolSpec representation; send them through unchanged.+ function_tools = [tool for tool in tools if not _is_hosted_responses_tool(tool)]++ # tool_choice dicts that aren't the chat-nested function form are already+ # Responses-native (flat function, hosted, allowed_tools, ...); send them+ # through unchanged.+ responses_native_tool_choice = None+ if isinstance(request.get("tool_choice"), dict) and "function" not in request["tool_choice"]:+ responses_native_tool_choice = request.pop("tool_choice")++ lm_request = LMRequest.from_call(model=model, messages=messages, tools=function_tools or None, **request)+ # The old converter never validated reasoning/temperature combinations+ # client-side; keep the provider as the authority on this door.+ data = to_openai_responses_request(lm_request, enforce_reasoning_temperature=False)+ if tools:+ normalized_function_tools = iter(data.pop("tools", []))+ data["tools"] = [+ tool if _is_hosted_responses_tool(tool) else next(normalized_function_tools) for tool in tools+ ]+ if responses_native_tool_choice is not None:+ data["tool_choice"] = responses_native_tool_choice+ return data+++def _is_hosted_responses_tool(tool: Any) -> bool:+ return isinstance(tool, dict) and "function" not in tool and tool.get("type") not in (None, "function")+++def _sanitize_legacy_message(message: dict[str, Any]) -> dict[str, Any]:+ """Normalize one legacy message the old converter would have passed through.++ Strips provider-SDK output fields that are not message inputs and rewrites+ Responses-native content blocks into their chat forms so the mapper can+ re-emit them with role-correct direction."""- if item.get("type") == "image_url":- image_url = item.get("image_url", {}).get("url", "")- return {- "type": "input_image",- "image_url": image_url,- }- elif item.get("type") == "text":- return {- "type": "input_text",- "text": item.get("text", ""),- }- elif item.get("type") == "file":- file = item.get("file", {})- return {- "type": "input_file",- "file_data": file.get("file_data"),- "filename": file.get("filename"),- "file_id": file.get("file_id"),- }-- # For other items, return as-is- return item+ if not isinstance(message, dict):+ return message+ message_keys = {"role", "content", "name", "metadata", "tool_calls", "tool_call_id"}+ cleaned = {key: value for key, value in message.items() if key in message_keys}+ content = cleaned.get("content")+ if isinstance(content, list):+ cleaned["content"] = [_normalize_legacy_content_block(block) for block in content]+ return cleaned+++def _normalize_legacy_content_block(block: Any) -> Any:+ if not isinstance(block, dict):+ return block+ block_type = block.get("type")+ if block_type in ("input_text", "output_text"):+ return {"type": "text", "text": block.get("text", "")}+ if block_type == "input_image" and isinstance(block.get("image_url"), str):+ return {"type": "image_url", "image_url": {"url": block["image_url"]}}+ if block_type == "input_file":+ file = {key: block[key] for key in ("file_data", "file_id", "filename") if block.get(key) is not None}+ return {"type": "file", "file": file}+ return blockdef _add_dspy_identifier_to_headers(headers: dict[str, Any] | None = None):diff --git a/dspy/clients/openai_format.py b/dspy/clients/openai_format.pyindex b6e13e21b..2a18b136e 100644--- a/dspy/clients/openai_format.py+++ b/dspy/clients/openai_format.py@@ -55,6 +55,7 @@ __all__ = ["to_openai_chat_request","to_openai_responses_request","to_openai_text_request",+ "tool_to_openai_responses","completion_to_lm_response","responses_to_lm_response","provider_tool_call_to_part",@@ -120,18 +121,20 @@ def message_to_openai_chat(message: LMMessage) -> dict[str, Any]:# ----------------------------------------------------------------------------def to_openai_responses_request(request: LMRequest) -> dict[str, Any]:+def to_openai_responses_request(request: LMRequest, *, enforce_reasoning_temperature: bool = True) -> dict[str, Any]:"""Convert a normalized DSPy request into Responses API kwargs."""config = request.configdata: dict[str, Any] = {"model": request.model,"input": [item for message in request.messages for item in message_to_responses_input_items(message)],}- data.update(responses_config_kwargs(config, model=request.model))+ data.update(+ responses_config_kwargs(config, model=request.model, enforce_reasoning_temperature=enforce_reasoning_temperature)+ )if config.tool_choice is not None:- data.update(tool_choice_to_openai(config.tool_choice))+ data.update(tool_choice_to_openai_responses(config.tool_choice))if request.tools:- data["tools"] = [tool_to_openai(tool) for tool in request.tools]+ data["tools"] = [tool_to_openai_responses(tool) for tool in request.tools]return data@@ -146,7 +149,7 @@ def message_to_responses_input_items(message: LMMessage) -> list[dict[str, Any]]tool_calls = [part for part in message.parts if isinstance(part, LMToolCallPart)]content_parts = [part for part in message.parts if not isinstance(part, LMToolCallPart)]- content = parts_to_responses_content(content_parts)+ content = parts_to_responses_content(content_parts, role=message.role)items: list[dict[str, Any]] = []if content or message.role != "assistant" or not tool_calls:@@ -160,11 +163,18 @@ def message_to_responses_input_items(message: LMMessage) -> list[dict[str, Any]]return items-def parts_to_responses_content(parts: list[Any]) -> list[dict[str, Any]]:+def parts_to_responses_content(parts: list[Any], role: str = "user") -> list[dict[str, Any]]:blocks = parts_to_openai_content(parts)if isinstance(blocks, str):- return [{"type": "input_text", "text": blocks}]- return [content_block_to_responses(block) for block in blocks]+ return [{"type": _responses_text_type(role), "text": blocks}]+ return [content_block_to_responses(block, role) for block in blocks]+++def _responses_text_type(role: str) -> str:+ # The Responses API types text by direction, not just by role slot: assistant+ # input items replay *model output*, so their text must be "output_text";+ # "input_text" on an assistant item is rejected with a 400.+ return "output_text" if role == "assistant" else "input_text"def tool_call_to_responses_input(tool_call_part: LMToolCallPart) -> dict[str, Any]:@@ -177,10 +187,10 @@ def tool_call_to_responses_input(tool_call_part: LMToolCallPart) -> dict[str, Anreturn item-def content_block_to_responses(block: dict[str, Any]) -> dict[str, Any]:+def content_block_to_responses(block: dict[str, Any], role: str = "user") -> dict[str, Any]:block_type = block.get("type")if block_type == "text":- return {"type": "input_text", "text": block.get("text", "")}+ return {"type": _responses_text_type(role), "text": block.get("text", "")}if block_type == "image_url":image_url = block.get("image_url", {})out = {"type": "input_image", "image_url": image_url.get("url", "")}@@ -343,11 +353,34 @@ def binary_to_openai(binary: LMBinaryPart) -> dict[str, Any]:return {"type": "file", "file": file_data}+_TOOL_SPEC_WIRE_KEYS = {"type", "name", "description", "parameters", "strict"}+++def _tool_provider_extras(tool: LMToolSpec) -> dict[str, Any]:+ return {key: value for key, value in tool.provider_data.items() if key not in _TOOL_SPEC_WIRE_KEYS}++def tool_to_openai(tool: LMToolSpec) -> dict[str, Any]:+ # provider_data holds function-scoped extras; each dialect emits them where+ # it puts function fields — nested under "function" here, flattened to the+ # top level in the Responses shape.data = {"type": "function", "function": {"name": tool.name, "parameters": tool.parameters}}if tool.description is not None:data["function"]["description"] = tool.description- data.update(tool.provider_data)+ if tool.strict is not None:+ data["function"]["strict"] = tool.strict+ data["function"].update(_tool_provider_extras(tool))+ return data+++def tool_to_openai_responses(tool: LMToolSpec) -> dict[str, Any]:+ """Convert a normalized tool spec into Responses API function-tool shape."""+ data = {"type": "function", "name": tool.name, "parameters": tool.parameters}+ if tool.description is not None:+ data["description"] = tool.description+ if tool.strict is not None:+ data["strict"] = tool.strict+ data.update(_tool_provider_extras(tool))return data@@ -366,11 +399,33 @@ def tool_choice_to_openai(choice: LMToolChoice) -> dict[str, Any]:return data+def tool_choice_to_openai_responses(choice: LMToolChoice) -> dict[str, Any]:+ if choice.allowed:+ if len(choice.allowed) != 1 or choice.mode not in {"required", "auto"}:+ raise ValueError(+ "OpenAI Responses tool_choice only supports constraining to a single allowed tool "+ "with mode 'required' or 'auto'."+ )+ data: dict[str, Any] = {"tool_choice": {"type": "function", "name": choice.allowed[0]}}+ else:+ data = {"tool_choice": choice.mode}+ if choice.parallel is not None:+ data["parallel_tool_calls"] = choice.parallel+ return data+++# provider_data on parsed tool calls echoes raw provider fields (the fc_* item+# id, status) and parser diagnostics; none of these may override the canonical+# wire fields or leak into requests.+_TOOL_CALL_INTERNAL_KEYS = {"id", "call_id", "status", "raw_arguments", "arguments_parse_error"}++def assistant_tool_call_to_openai(call: LMToolCallPart) -> dict[str, Any]:- data = {"type": "function", "function": {"name": call.name, "arguments": json.dumps(call.args)}}+ data = {key: value for key, value in call.provider_data.items() if key not in _TOOL_CALL_INTERNAL_KEYS}+ data["type"] = "function"+ data["function"] = {"name": call.name, "arguments": json.dumps(call.args)}if call.id is not None:data["id"] = call.id- data.update(call.provider_data)return data@@ -404,10 +459,13 @@ def common_config_kwargs(config: LMConfig, *, model: str | None = None, endpointreturn data-def responses_config_kwargs(config: LMConfig, *, model: str | None = None) -> dict[str, Any]:+def responses_config_kwargs(+ config: LMConfig, *, model: str | None = None, enforce_reasoning_temperature: bool = True+) -> dict[str, Any]:"""Convert shared DSPy config fields into Responses API kwargs."""data = dict(config.extensions) if config.extensions else {}- _validate_openai_reasoning_temperature(config, model=model, endpoint="responses")+ if enforce_reasoning_temperature:+ _validate_openai_reasoning_temperature(config, model=model, endpoint="responses")for key in ("temperature", "top_p"):value = getattr(config, key)if value is not None:@@ -509,10 +567,41 @@ def prompt_cache_to_kwargs(cache: Any) -> dict[str, Any]:def response_format_to_responses(value: Any) -> Any:if isinstance(value, type) and issubclass(value, pydantic.BaseModel):- return {"name": value.__name__, "type": "json_schema", "schema": value.model_json_schema()}+ return {+ "name": value.__name__,+ "type": "json_schema",+ "schema": _close_object_schemas(value.model_json_schema()),+ }return value+def _close_object_schemas(schema: Any) -> Any:+ """Recursively supply ``additionalProperties: false`` on object schemas.++ The Responses API rejects ``json_schema`` formats whose object schemas leave+ ``additionalProperties`` unspecified; pydantic's ``model_json_schema()``+ omits it. Schemas that already set it (e.g. ``dict[str, T]`` fields) are+ left untouched. Recursion follows JSON Schema keyword positions only, so+ schema-shaped values inside ``default``/``examples``/``const`` are never+ rewritten.+ """+ if not isinstance(schema, dict):+ return schema+ closed = dict(schema)+ for key in ("items", "additionalProperties", "not", "if", "then", "else", "contains", "propertyNames"):+ if isinstance(closed.get(key), dict):+ closed[key] = _close_object_schemas(closed[key])+ for key in ("items", "prefixItems", "anyOf", "oneOf", "allOf"):+ if isinstance(closed.get(key), list):+ closed[key] = [_close_object_schemas(item) for item in closed[key]]+ for key in ("properties", "patternProperties", "$defs", "definitions"):+ if isinstance(closed.get(key), dict):+ closed[key] = {name: _close_object_schemas(sub) for name, sub in closed[key].items()}+ if closed.get("type") == "object" and "additionalProperties" not in closed:+ closed["additionalProperties"] = False+ return closed++def responses_tool_output_text(content: Any) -> str:if isinstance(content, str):return contentdiff --git a/dspy/core/types.py b/dspy/core/types.pyindex e0884286c..62f708ef2 100644--- a/dspy/core/types.py+++ b/dspy/core/types.py@@ -296,6 +296,7 @@ class LMToolSpec(BaseModel):name: strdescription: str | None = Noneparameters: dict[str, Any] = Field(default_factory=dict)+ strict: bool | None = Nonemetadata: dict[str, Any] = Field(default_factory=dict)provider_data: dict[str, Any] = Field(default_factory=dict)@@ -1839,7 +1840,9 @@ def _tool_call_from_openai(tool_call: Any) -> LMToolCallPart:if not isinstance(function, Mapping):function = {}- args = function.get("arguments", {})+ args = function.get("arguments")+ if args is None:+ args = tool_call.get("arguments", {})if isinstance(args, str):args = _parse_json_object(args)elif isinstance(args, Mapping):@@ -1848,7 +1851,7 @@ def _tool_call_from_openai(tool_call: Any) -> LMToolCallPart:args = {}return LMToolCallPart(- id=tool_call.get("id"),+ id=tool_call.get("call_id") or tool_call.get("id"),name=function.get("name") or tool_call.get("name") or "",args=args,)@@ -1888,9 +1891,19 @@ def _audio_dict_to_part(audio: dict[str, Any]) -> LMAudioPart:def _binary_dict_to_part(file: dict[str, Any]) -> LMBinaryPart:- if file.get("file_data") is not None:- media_type, data = _split_data_uri(file["file_data"])- return LMBinaryPart(data=data, media_type=media_type, filename=file.get("filename"))+ # Stash the original block whenever the typed model can't re-emit it+ # losslessly: both file_data and file_id present (a part holds one source),+ # or file_data that isn't a data: URI (re-wrapping would invent a+ # media type the caller never declared).+ file_data = file.get("file_data")+ needs_legacy_block = file_data is not None and (+ file.get("file_id") is not None or not str(file_data).startswith("data:")+ )+ legacy_block = {"type": "file", "file": dict(file)} if needs_legacy_block else None+ if file_data is not None:+ media_type, data = _split_data_uri(file_data)+ metadata = {"legacy_content_block": legacy_block} if legacy_block is not None else {}+ return LMBinaryPart(data=data, media_type=media_type, filename=file.get("filename"), metadata=metadata)if file.get("data") is not None:media_type, data = _split_data_uri(file["data"])return LMBinaryPart(data=data, media_type=media_type, filename=file.get("filename"))@@ -1965,13 +1978,28 @@ def _coerce_tool_spec(tool: Any) -> LMToolSpec:if isinstance(tool, dict):if "function" in tool:function = tool["function"]+ # provider_data carries function-scoped extras (see tool_to_openai*);+ # top-level extras on the nested form are folded into the same scope.provider_data = {key: value for key, value in tool.items() if key not in {"type", "function"}}+ provider_data.update(+ {+ key: value+ for key, value in function.items()+ if key not in {"name", "description", "parameters", "strict"}+ }+ )return LMToolSpec(name=function.get("name"),description=function.get("description"),parameters=function.get("parameters", {}),+ strict=function.get("strict"),provider_data=provider_data,)+ known_fields = set(LMToolSpec.model_fields)+ extras = {key: value for key, value in tool.items() if key not in known_fields}+ if extras:+ tool = {key: value for key, value in tool.items() if key in known_fields}+ tool["provider_data"] = {**tool.get("provider_data", {}), **extras}return LMToolSpec(**tool)raise TypeError(f"Cannot convert {type(tool)!r} to LMToolSpec.")diff --git a/tests/adapters/test_chat_adapter.py b/tests/adapters/test_chat_adapter.pyindex 5b4d4c0d5..c4db04d11 100644--- a/tests/adapters/test_chat_adapter.py+++ b/tests/adapters/test_chat_adapter.py@@ -2715,22 +2715,91 @@ def test_empty_string_content_raises_adapter_parse_error():cot(question="test")-def test_tool_call_with_null_content_does_not_raise():- """Tool-call-only responses legitimately have content=None.[email protected]("output", [+ {"text": None, "tool_calls": [+ {"function": {"name": "search", "arguments": '{"query": "test"}'}, "id": "call_1", "type": "function"}+ ]},+ {"tool_calls": [+ {"function": {"name": "search", "arguments": '{"query": "test"}'}, "id": "call_1", "type": "function"}+ ]},+])+def test_tool_call_with_null_or_missing_content_does_not_raise(output):+ """Tool-call-only responses legitimately have content=None or no text key._call_postprocess must NOT raise when tool_calls are present."""adapter = dspy.ChatAdapter(use_native_function_calling=True)sig_cls = dspy.Signature("question, tools: list[dspy.Tool] -> answer, tool_calls: dspy.ToolCalls")- outputs = [{"text": None, "tool_calls": [- {"function": {"name": "search", "arguments": '{"query": "test"}'}, "id": "call_1", "type": "function"}- ]}]-- result = adapter._call_postprocess(sig_cls, sig_cls, outputs, None, {})+ result = adapter._call_postprocess(sig_cls, sig_cls, [output], None, {})assert result is not Noneassert len(result) == 1assert result[0]["tool_calls"].tool_calls[0].id == "call_1"+def test_responses_model_native_tool_calling_round_trips_tool_only_output():+ from litellm.types.llms.openai import ResponseAPIUsage, ResponsesAPIResponse++ class MySignature(dspy.Signature):+ question: str = dspy.InputField()+ tools: list[dspy.Tool] = dspy.InputField()+ tool_calls: dspy.ToolCalls = dspy.OutputField()++ def search(query: str) -> str:+ return query++ api_response = ResponsesAPIResponse(+ id="resp_1",+ created_at=0.0,+ error=None,+ incomplete_details=None,+ instructions=None,+ model="openai/dspy-test-model",+ object="response",+ output=[+ {+ "type": "function_call",+ "name": "search",+ "arguments": json.dumps({"query": "cats"}),+ "call_id": "call_1",+ "status": "completed",+ "id": "fc_1",+ }+ ],+ metadata={},+ parallel_tool_calls=False,+ temperature=1.0,+ tool_choice="auto",+ tools=[],+ top_p=1.0,+ max_output_tokens=None,+ previous_response_id=None,+ reasoning=None,+ status="completed",+ text=None,+ truncation="disabled",+ usage=ResponseAPIUsage(input_tokens=1, output_tokens=1, total_tokens=2),+ user=None,+ )++ adapter = dspy.ChatAdapter(use_native_function_calling=True)+ lm = dspy.LM("openai/dspy-test-model", model_type="responses", cache=False)++ with mock.patch("litellm.supports_function_calling", return_value=True), mock.patch(+ "litellm.responses", return_value=api_response+ ) as responses:+ result = adapter(lm, {}, MySignature, [], {"question": "find cats", "tools": [dspy.Tool(search)]})++ assert result == [+ {"tool_calls": dspy.ToolCalls(tool_calls=[dspy.ToolCalls.ToolCall(id="call_1", name="search", args={"query": "cats"})])}+ ]+ assert responses.call_args.kwargs["tools"] == [+ {+ "type": "function",+ "name": "search",+ "parameters": {"type": "object", "properties": {"query": {"type": "string"}}, "required": ["query"]},+ }+ ]++def test_tool_call_with_unstructured_content_does_not_raise():"""Provider tool calls are authoritative even when content is not adapter-formatted."""adapter = dspy.ChatAdapter(use_native_function_calling=True)diff --git a/tests/clients/test_cache.py b/tests/clients/test_cache.pyindex bae3def2e..105dbbece 100644--- a/tests/clients/test_cache.py+++ b/tests/clients/test_cache.py@@ -533,3 +533,24 @@ def test_safe_types_rejects_non_types(tmp_path):restrict_pickle=True,safe_types=["not_a_type"],)+++class _StrictProviderResponse(pydantic.BaseModel):+ """Mimics strict provider SDK models (e.g. litellm's ResponsesAPIResponse)."""++ model_config = pydantic.ConfigDict(extra="forbid")++ output: list[dict]+ usage: dict+++def test_prepare_cached_response_marks_strict_models_as_cache_hit(cache):+ response = _StrictProviderResponse(output=[{"type": "message"}], usage={"total_tokens": 3})++ prepared = cache._prepare_cached_response(response)++ assert prepared.cache_hit is True+ assert prepared.usage == {}+ # The original response is not mutated.+ assert getattr(response, "cache_hit", False) is False+ assert response.usage == {"total_tokens": 3}diff --git a/tests/clients/test_lm.py b/tests/clients/test_lm.pyindex bcb1430f2..e740a0e8b 100644--- a/tests/clients/test_lm.py+++ b/tests/clients/test_lm.py@@ -1256,6 +1256,45 @@ def test_responses_api_tool_calls(litellm_test_server, provider_fields, expectedassert dspy_responses.call_args.kwargs["model"] == "openai/dspy-test-model"+def test_responses_api_cache_hit_preserves_outputs_and_skips_usage(tmp_path):+ api_response = make_response(+ output_blocks=[+ ResponseOutputMessage(+ id="msg_1",+ type="message",+ role="assistant",+ status="completed",+ content=[{"type": "output_text", "text": "cached answer", "annotations": []}],+ ),+ ResponseReasoningItem(+ id="reasoning_1",+ type="reasoning",+ summary=[Summary(type="summary_text", text="cached reasoning")],+ ),+ ],+ )++ original_cache = dspy.cache+ dspy.configure_cache(enable_disk_cache=True, enable_memory_cache=True, disk_cache_dir=tmp_path / ".dspy_cache")+ try:+ with mock.patch("litellm.responses", autospec=True, return_value=api_response) as responses:+ lm = dspy.LM("openai/dspy-test-model", model_type="responses")+ with track_usage() as first_usage:+ first = lm("cache me")+ with track_usage() as second_usage:+ second = lm("cache me")++ assert first == [{"text": "cached answer", "reasoning_content": "cached reasoning"}]+ assert second == first+ assert responses.call_count == 1+ # The fresh call records usage; the cache hit must not.+ assert len(first_usage.usage_data) == 1+ assert len(second_usage.usage_data) == 0+ assert lm.history[-1]["usage"] == {}+ finally:+ dspy.cache = original_cache++def test_reasoning_effort_responses_api():"""Test that reasoning_effort gets normalized to reasoning format for Responses API."""with mock.patch("litellm.responses") as mock_responses:@@ -1510,13 +1549,15 @@ def test_responses_api_preserves_multi_message_structure():{"role": "user", "content": "What is 2+2?"},{"role": "assistant", "content": "4"},{"role": "user", "content": "And 3+3?"},+ {"role": "assistant", "content": [{"type": "text", "text": "6"}]},+ {"role": "user", "content": "And 4+4?"},],}result = _convert_chat_request_to_responses_request(request)assert "input" in result- assert len(result["input"]) == 4+ assert len(result["input"]) == 6assert result["input"][0]["role"] == "system"assert result["input"][0]["content"] == [{"type": "input_text", "text": "You are a helpful assistant."}]@@ -1524,11 +1565,17 @@ def test_responses_api_preserves_multi_message_structure():assert result["input"][1]["role"] == "user"assert result["input"][1]["content"] == [{"type": "input_text", "text": "What is 2+2?"}]+ # Assistant history replays model output, which the Responses API types as+ # "output_text"; "input_text" on an assistant item is rejected with a 400.assert result["input"][2]["role"] == "assistant"- assert result["input"][2]["content"] == [{"type": "input_text", "text": "4"}]+ assert result["input"][2]["content"] == [{"type": "output_text", "text": "4"}]assert result["input"][3]["role"] == "user"assert result["input"][3]["content"] == [{"type": "input_text", "text": "And 3+3?"}]+ assert result["input"][4]["role"] == "assistant"+ assert result["input"][4]["content"] == [{"type": "output_text", "text": "6"}]+ assert result["input"][5]["role"] == "user"+ assert result["input"][5]["content"] == [{"type": "input_text", "text": "And 4+4?"}]def test_responses_api_with_image_input():@@ -1643,7 +1690,7 @@ def test_responses_api_with_pydantic_model_input():assert response_format == {"name": TestModel.__name__,"type": "json_schema",- "schema": TestModel.model_json_schema(),+ "schema": {**TestModel.model_json_schema(), "additionalProperties": False},}@@ -1795,3 +1842,239 @@ async def test_streaming_passes_headers_correctly():mock_acompletion.assert_called_once()call_kwargs = mock_acompletion.call_args.kwargsassert call_kwargs["headers"]["Authorization"] == "Bearer my-custom-token"+++# ---------------------------------------------------------------------------+# Responses API request contract: the shapes DSPy must emit for tools,+# tool_choice, messages, and config on model_type="responses".+# ---------------------------------------------------------------------------+++def _chat_shaped_weather_tool():+ return {+ "type": "function",+ "function": {+ "name": "get_weather",+ "description": "Get weather.",+ "parameters": {+ "type": "object",+ "properties": {"city": {"type": "string"}},+ "required": ["city"],+ },+ },+ }+++def _responses_text_response():+ return make_response(+ [+ {+ "type": "message",+ "role": "assistant",+ "content": [{"type": "output_text", "text": "OK", "annotations": []}],+ "id": "msg_1",+ "status": "completed",+ }+ ]+ )+++class ContractSchema(pydantic.BaseModel):+ answer: str+++def test_openai_format_responses_request_maps_tools_choices_and_config():+ from dspy.clients.openai_format import to_openai_responses_request+ from dspy.core.types import LMRequest++ tool = _chat_shaped_weather_tool()+ tool["function"]["strict"] = True+ request = LMRequest.from_call(+ model="openai/gpt-5-mini",+ messages=[{"role": "user", "content": "What is the weather in Paris?"}],+ tools=[tool],+ tool_choice={"type": "function", "function": {"name": "get_weather"}},+ parallel_tool_calls=False,+ reasoning={"effort": "low", "summary": "auto"},+ response_format=ContractSchema,+ max_tokens=123,+ )++ data = to_openai_responses_request(request)++ assert data["input"] == [+ {+ "role": "user",+ "content": [{"type": "input_text", "text": "What is the weather in Paris?"}],+ }+ ]+ assert data["tools"] == [+ {+ "type": "function",+ "name": "get_weather",+ "description": "Get weather.",+ "parameters": {+ "type": "object",+ "properties": {"city": {"type": "string"}},+ "required": ["city"],+ },+ "strict": True,+ }+ ]+ assert data["tool_choice"] == {"type": "function", "name": "get_weather"}+ assert data["parallel_tool_calls"] is False+ assert data["reasoning"] == {"effort": "low", "summary": "auto"}+ assert data["max_output_tokens"] == 123+ # Closed schema, not a raw model_json_schema(): the Responses API rejects+ # schemas without explicit additionalProperties: false.+ assert data["text"]["format"] == {+ "type": "json_schema",+ "name": "ContractSchema",+ "schema": {**ContractSchema.model_json_schema(), "additionalProperties": False},+ }+++def test_responses_request_converts_assistant_tool_calls_and_tool_results():+ from dspy.clients.openai_format import to_openai_responses_request+ from dspy.core.types import LMRequest++ request = LMRequest.from_call(+ model="openai/gpt-5-mini",+ messages=[+ {"role": "user", "content": "What is the weather?"},+ {+ "role": "assistant",+ "content": None,+ "tool_calls": [+ {+ "id": "call_1",+ "type": "function",+ "function": {"name": "get_weather", "arguments": json.dumps({"city": "Paris"})},+ }+ ],+ },+ {"role": "tool", "tool_call_id": "call_1", "name": "get_weather", "content": "sunny"},+ ],+ )++ data = to_openai_responses_request(request)++ assert data["input"] == [+ {"role": "user", "content": [{"type": "input_text", "text": "What is the weather?"}]},+ {+ "type": "function_call",+ "name": "get_weather",+ "arguments": json.dumps({"city": "Paris"}),+ "call_id": "call_1",+ },+ {"type": "function_call_output", "output": "sunny", "call_id": "call_1"},+ ]+++def test_lm_responses_passes_hosted_tools_through_unchanged():+ response = _responses_text_response()+ hosted_tool = {"type": "web_search", "search_context_size": "low"}+ flat_tool = {+ "type": "function",+ "name": "get_weather",+ "parameters": {"type": "object", "properties": {"city": {"type": "string"}}},+ "strict": True,+ }++ with mock.patch("litellm.responses", autospec=True, return_value=response) as responses:+ lm = dspy.LM("openai/dspy-test-model", model_type="responses", cache=False)+ lm("What is in the news?", tools=[hosted_tool, _chat_shaped_weather_tool(), flat_tool])++ sent_tools = responses.call_args.kwargs["tools"]+ assert sent_tools[0] == hosted_tool+ assert sent_tools[1]["name"] == "get_weather"+ assert sent_tools[2] == flat_tool+++def test_lm_responses_passes_native_tool_choice_shapes_through():+ response = _responses_text_response()+ for native_choice in (+ {"type": "function", "name": "get_weather"},+ {"type": "web_search_preview"},+ {"type": "allowed_tools", "mode": "auto", "tools": [{"type": "function", "name": "get_weather"}]},+ ):+ with mock.patch("litellm.responses", autospec=True, return_value=response) as responses:+ lm = dspy.LM("openai/dspy-test-model", model_type="responses", cache=False)+ lm("What is the weather?", tools=[_chat_shaped_weather_tool()], tool_choice=native_choice)++ assert responses.call_args.kwargs["tool_choice"] == native_choice+++def test_lm_responses_tolerates_native_content_and_sdk_message_dumps():+ response = _responses_text_response()++ with mock.patch("litellm.responses", autospec=True, return_value=response) as responses:+ lm = dspy.LM("openai/dspy-test-model", model_type="responses", cache=False)+ lm(+ messages=[+ {"role": "user", "content": [{"type": "input_text", "text": "Say hi."}]},+ # An assistant message dict dumped from an OpenAI SDK response.+ {"role": "assistant", "content": "hi", "refusal": None, "annotations": [], "function_call": None},+ {"role": "user", "content": [{"type": "output_text", "text": "Again."}]},+ {+ "id": "msg_1",+ "type": "message",+ "status": "completed",+ "role": "assistant",+ "content": [{"type": "output_text", "text": "hello", "annotations": []}],+ },+ ]+ )++ sent = responses.call_args.kwargs["input"]+ assert sent[0]["content"] == [{"type": "input_text", "text": "Say hi."}]+ assert sent[1] == {"role": "assistant", "content": [{"type": "output_text", "text": "hi"}]}+ assert sent[2]["content"] == [{"type": "input_text", "text": "Again."}]+ assert sent[3] == {"role": "assistant", "content": [{"type": "output_text", "text": "hello"}]}+++def test_lm_responses_explicit_reasoning_wins_over_constructor_effort():+ response = _responses_text_response()++ with mock.patch("litellm.responses", autospec=True, return_value=response) as responses:+ lm = dspy.LM("openai/dspy-test-model", model_type="responses", cache=False, reasoning_effort="low")+ lm("Say hi.", reasoning={"effort": "high"})++ sent = responses.call_args.kwargs+ assert sent["reasoning"] == {"effort": "high"}+ assert "reasoning_effort" not in sent+++def test_lm_responses_forwards_raw_base64_file_data_verbatim():+ response = _responses_text_response()+ raw_base64 = "JVBERi0xLjQK"++ with mock.patch("litellm.responses", autospec=True, return_value=response) as responses:+ lm = dspy.LM("openai/dspy-test-model", model_type="responses", cache=False)+ lm(+ messages=[+ {+ "role": "user",+ "content": [+ {"type": "text", "text": "Read this."},+ {"type": "file", "file": {"file_data": raw_base64, "filename": "doc.pdf"}},+ ],+ }+ ]+ )++ file_item = responses.call_args.kwargs["input"][0]["content"][1]+ assert file_item["type"] == "input_file"+ assert file_item["file_data"] == raw_base64+++def test_lm_responses_does_not_validate_reasoning_temperature_client_side():+ response = _responses_text_response()++ with mock.patch("litellm.responses", autospec=True, return_value=response) as responses:+ lm = dspy.LM("openai/gpt-5-nano", model_type="responses", cache=False, max_tokens=16000)+ lm("Say hi.", temperature=0.7, reasoning_effort="low")++ sent = responses.call_args.kwargs+ assert sent["temperature"] == 0.7+ assert sent["reasoning"] == {"effort": "low", "summary": "auto"}diff --git a/tests/clients/test_lm_direct_live.py b/tests/clients/test_lm_direct_live.pyindex b0ccd3f6d..01fe2e67f 100644--- a/tests/clients/test_lm_direct_live.py+++ b/tests/clients/test_lm_direct_live.py@@ -8,8 +8,10 @@ They are intentionally flat rather than parametrized so each test can be run indworkflow. Each test skips unless the required provider credential is available."""+import jsonimport os+import pydanticimport pytestimport dspy@@ -309,3 +311,172 @@ def test_live_gemini_chat_direct_reuse_lm_response_as_assistant_turn():assert "dspy" in _text(first).lower()assert "dspy" in _text(follow_up).lower()+++# ---------------------------------------------------------------------------+# Responses dialect probes.+#+# The Responses request dialect is enforced only by OpenAI's server-side+# validator: the SDK's request types accept shapes the server rejects, and+# mocked tests encode the mapper's own beliefs. One minimal real-API request+# per mapper-supported shape catches the #9943/#9652 class before release.+# Each probe asserts acceptance plus the smallest semantic check; round-trip+# probes cover what acceptance alone can't (e.g. referenceable tool-call ids).+# ---------------------------------------------------------------------------+++def responses_lm():+ _require_env("OPENAI_API_KEY")+ return dspy.LM(+ os.getenv("LM_FOR_TEST_RESPONSES", "openai/gpt-5-nano"),+ model_type="responses",+ cache=False,+ temperature=1.0,+ max_tokens=16000,+ )+++WEATHER_TOOL_CHAT = {+ "type": "function",+ "function": {+ "name": "get_weather",+ "description": "Get the current weather for a city.",+ "parameters": {+ "type": "object",+ "properties": {"city": {"type": "string"}},+ "required": ["city"],+ },+ },+}++# 1x1 transparent PNG.+TINY_PNG_URI = (+ "data:image/png;base64,"+ "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="+)+++def _first_tool_call(outputs):+ out = outputs[0]+ assert isinstance(out, dict) and out.get("tool_calls"), f"expected a tool call, got: {out!r}"+ call = out["tool_calls"][0]+ name = call.get("name") or call.get("function", {}).get("name")+ arguments = call.get("arguments") or call.get("function", {}).get("arguments")+ call_id = call.get("call_id") or call.get("id")+ return name, json.loads(arguments), call_id++[email protected]_call+def test_probe_history_roles_and_text_content_forms(responses_lm):+ outputs = responses_lm(+ messages=[+ {"role": "developer", "content": "Answer with one word."},+ {"role": "user", "content": [{"type": "text", "text": "Say apple."}]},+ {"role": "assistant", "content": "apple"},+ {"role": "user", "content": "Now say banana."},+ {"role": "assistant", "content": [{"type": "text", "text": "banana"}]},+ {"role": "user", "content": [{"type": "text", "text": "Now say cherry."}]},+ ]+ )+ assert outputs++[email protected]_call+def test_probe_image_content(responses_lm):+ outputs = responses_lm(+ messages=[+ {+ "role": "user",+ "content": [+ {"type": "text", "text": "One word: what color dominates this image?"},+ {"type": "image_url", "image_url": {"url": TINY_PNG_URI}},+ ],+ }+ ]+ )+ assert outputs++[email protected]_call+def test_probe_flat_tool_with_strict(responses_lm):+ flat_strict = {+ "type": "function",+ "name": "get_weather",+ "description": "Get the current weather for a city.",+ "parameters": {+ "type": "object",+ "properties": {"city": {"type": "string"}},+ "required": ["city"],+ "additionalProperties": False,+ },+ "strict": True,+ }+ outputs = responses_lm(+ "Talk about the weather in Oslo.",+ tools=[flat_strict],+ tool_choice={"type": "function", "name": "get_weather"},+ )+ name, args, _ = _first_tool_call(outputs)+ assert name == "get_weather"+ assert args == {"city": "Oslo"}++[email protected]_call+def test_probe_hosted_web_search_tool(responses_lm):+ # The pinned openai SDK models only the "web_search_preview" hosted-tool+ # shape; the newer "web_search" shape fails in litellm's response parsing.+ outputs = responses_lm("Search the web: what year is it? Answer briefly.", tools=[{"type": "web_search_preview"}])+ assert outputs++[email protected]_call+def test_probe_tool_choice_none_suppresses_calls(responses_lm):+ outputs = responses_lm(+ "What is the weather in Lima? Use the tool.",+ tools=[WEATHER_TOOL_CHAT],+ tool_choice="none",+ )+ out = outputs[0]+ assert not (isinstance(out, dict) and out.get("tool_calls"))++[email protected]_call+def test_probe_response_format_with_reasoning(responses_lm):+ class Answer(pydantic.BaseModel):+ word: str++ outputs = responses_lm("Reply with the word apple.", response_format=Answer, reasoning_effort="low")+ text = outputs[0]["text"] if isinstance(outputs[0], dict) else outputs[0]+ assert Answer.model_validate_json(text).word++[email protected]_call+def test_probe_tool_round_trip_ids_are_referenceable(responses_lm):+ """Acceptance alone can't catch id bugs: a request that sends back the+ wrong id (the fc_* item id instead of call_id) fails only on this second+ turn."""+ outputs = responses_lm(+ "Talk about the weather in Berlin.",+ tools=[WEATHER_TOOL_CHAT],+ tool_choice={"type": "function", "function": {"name": "get_weather"}},+ )+ name, _, call_id = _first_tool_call(outputs)+ tool_calls = outputs[0]["tool_calls"]++ followup = responses_lm(+ messages=[+ {"role": "user", "content": "What is the weather in Berlin? Use the tool."},+ {+ "role": "assistant",+ "content": None,+ # Replay the Responses-shaped output exactly as DSPy returned it.+ "tool_calls": tool_calls,+ },+ {"role": "tool", "tool_call_id": call_id, "name": name, "content": "It is 22C and sunny."},+ ],+ tools=[WEATHER_TOOL_CHAT],+ )+ final = followup[0]+ text = final["text"] if isinstance(final, dict) else final+ assert text and "22" in textdiff --git a/tests/core/test_types.py b/tests/core/test_types.pyindex 73a5ca366..4416c3688 100644--- a/tests/core/test_types.py+++ b/tests/core/test_types.py@@ -1,8 +1,11 @@+import json+import pydanticimport pytestfrom dspy.core.types import (LMAudioPart,+ LMBinaryPart,LMConfig,LMDocumentPart,LMHistoryEntry,@@ -142,6 +145,169 @@ def test_video_data_round_trips_through_history_messages():assert round_tripped.media_type == "video/mp4"+def test_tool_dicts_coerce_and_emit_strict_and_extras():+ from dspy.clients.openai_format import tool_to_openai, tool_to_openai_responses++ nested = LMRequest.from_call(+ model="openai/gpt-5-nano",+ messages=[{"role": "user", "content": "hi"}],+ tools=[+ {+ "type": "function",+ "function": {"name": "f", "parameters": {}, "strict": True, "vendor_key": 1},+ }+ ],+ ).tools[0]+ assert nested.strict is True+ assert nested.provider_data == {"vendor_key": 1}+ assert tool_to_openai(nested) == {+ "type": "function",+ "function": {"name": "f", "parameters": {}, "strict": True, "vendor_key": 1},+ }+ assert tool_to_openai_responses(nested) == {+ "type": "function",+ "name": "f",+ "parameters": {},+ "strict": True,+ "vendor_key": 1,+ }++ flat = LMRequest.from_call(+ model="openai/gpt-5-nano",+ messages=[{"role": "user", "content": "hi"}],+ tools=[{"type": "function", "name": "f", "parameters": {}, "strict": False, "custom_field": "x"}],+ ).tools[0]+ assert flat.strict is False+ assert flat.provider_data == {"custom_field": "x"}+++def test_parsed_tool_call_replays_with_call_id_not_item_id():+ from dspy.clients.openai_format import assistant_tool_call_to_openai, tool_call_to_responses_input++ message = LMMessage(+ role="assistant",+ content=None,+ tool_calls=[+ {+ "id": "fc_1",+ "type": "function_call",+ "name": "get_weather",+ "arguments": json.dumps({"city": "Paris"}),+ "call_id": "call_1",+ "status": "completed",+ }+ ],+ )+ part = message.parts[0]+ assert isinstance(part, LMToolCallPart)+ assert part.id == "call_1"+ assert part.name == "get_weather"+ assert part.args == {"city": "Paris"}++ part.provider_data = {"id": "fc_1", "call_id": "call_1", "status": "completed", "raw_arguments": "{}"}++ chat_call = assistant_tool_call_to_openai(part)+ assert chat_call["id"] == "call_1"+ assert "status" not in chat_call and "raw_arguments" not in chat_call++ responses_item = tool_call_to_responses_input(part)+ assert responses_item["call_id"] == "call_1"+ assert responses_item["arguments"] == json.dumps({"city": "Paris"})+++def test_tool_provider_extras_cannot_override_canonical_fields():+ from dspy.clients.openai_format import tool_to_openai, tool_to_openai_responses++ tool = LMRequest.from_call(+ model="openai/gpt-5-nano",+ messages=[{"role": "user", "content": "hi"}],+ tools=[+ {+ "type": "function",+ "name": "safe_name",+ "parameters": {"type": "object"},+ "strict": True,+ "provider_data": {+ "type": "web_search",+ "name": "overridden_name",+ "parameters": {"type": "string"},+ "strict": False,+ "vendor_key": 1,+ },+ }+ ],+ ).tools[0]++ assert tool_to_openai(tool) == {+ "type": "function",+ "function": {+ "name": "safe_name",+ "parameters": {"type": "object"},+ "strict": True,+ "vendor_key": 1,+ },+ }+ assert tool_to_openai_responses(tool) == {+ "type": "function",+ "name": "safe_name",+ "parameters": {"type": "object"},+ "strict": True,+ "vendor_key": 1,+ }+++def test_close_object_schemas_ignores_schema_shaped_defaults():+ from dspy.clients.openai_format import response_format_to_responses++ class WithDictDefault(pydantic.BaseModel):+ payload: dict = {"type": "object"}++ schema = response_format_to_responses(WithDictDefault)["schema"]+ assert schema["additionalProperties"] is False+ assert schema["properties"]["payload"]["default"] == {"type": "object"}+++def test_file_content_block_with_data_and_id_round_trips_losslessly_to_responses_format():+ from dspy.clients.openai_format import to_openai_responses_request++ message = LMMessage(+ role="user",+ content=[+ {+ "type": "file",+ "file": {+ "file_data": "data:application/pdf;base64,JVBERi0xLjQ=",+ "file_id": "file_123",+ "filename": "report.pdf",+ },+ }+ ],+ )+ part = message.parts[0]++ assert isinstance(part, LMBinaryPart)+ assert part.metadata["legacy_content_block"] == {+ "type": "file",+ "file": {+ "file_data": "data:application/pdf;base64,JVBERi0xLjQ=",+ "file_id": "file_123",+ "filename": "report.pdf",+ },+ }++ request = LMRequest(model="model", messages=[message])+ content = to_openai_responses_request(request)["input"][0]["content"]++ assert content == [+ {+ "type": "input_file",+ "file_data": "data:application/pdf;base64,JVBERi0xLjQ=",+ "file_id": "file_123",+ "filename": "report.pdf",+ }+ ]++def test_document_source_url_stays_url_and_round_trips_through_history_messages():message = LMMessage(role="user",