openai[patch]: accommodate change in image generation API (#31522)

OpenAI changed their API to require the `partial_images` parameter when
using image generation + streaming.

As described in https://github.com/langchain-ai/langchain/pull/31424, we
are ignoring partial images. Here, we accept the `partial_images`
parameter (as required by OpenAI), but emit a warning and continue to
ignore partial images.
This commit is contained in:
ccurme 2025-06-09 14:57:46 -04:00 committed by GitHub
parent ece9e31a7a
commit 575662d5f1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 6 deletions

View File

@ -3116,14 +3116,24 @@ def _construct_responses_api_payload(
if tool["type"] == "function" and "function" in tool:
new_tools.append({"type": "function", **tool["function"]})
else:
if tool["type"] == "image_generation":
# Handle partial images (not yet supported)
if "partial_images" in tool:
raise NotImplementedError(
"Partial image generation is not yet supported "
"via the LangChain ChatOpenAI client. Please "
"drop the 'partial_images' key from the image_generation "
"tool."
)
elif payload.get("stream") and "partial_images" not in tool:
# OpenAI requires this parameter be set; we ignore it during
# streaming.
tool["partial_images"] = 1
else:
pass
new_tools.append(tool)
if tool["type"] == "image_generation" and "partial_images" in tool:
raise NotImplementedError(
"Partial image generation is not yet supported "
"via the LangChain ChatOpenAI client. Please "
"drop the 'partial_images' key from the image_generation tool."
)
payload["tools"] = new_tools
if tool_choice := payload.pop("tool_choice", None):
# chat api: {"type": "function", "function": {"name": "..."}}