mirror of
https://github.com/hwchase17/langchain.git
synced 2026-06-09 10:17:00 +00:00
fix(openai): Fixing error that comes up using the Responses API with built-in tools and custom tools (#34136)
This commit is contained in:
@@ -3764,7 +3764,7 @@ def _construct_responses_api_payload(
|
|||||||
if payload.get("stream") and "partial_images" not in tool:
|
if payload.get("stream") and "partial_images" not in tool:
|
||||||
# OpenAI requires this parameter be set; we ignore it during
|
# OpenAI requires this parameter be set; we ignore it during
|
||||||
# streaming.
|
# streaming.
|
||||||
tool["partial_images"] = 1
|
tool = {**tool, "partial_images": 1}
|
||||||
else:
|
else:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
@@ -756,3 +756,38 @@ def test_responses_stream(output_version: str, expected_content: list[dict]) ->
|
|||||||
dumped = _strip_none(item.model_dump())
|
dumped = _strip_none(item.model_dump())
|
||||||
_ = dumped.pop("status", None)
|
_ = dumped.pop("status", None)
|
||||||
assert dumped == payload["input"][idx]
|
assert dumped == payload["input"][idx]
|
||||||
|
|
||||||
|
|
||||||
|
def test_responses_stream_with_image_generation_multiple_calls() -> None:
|
||||||
|
"""Test that streaming with image_generation tool works across multiple calls.
|
||||||
|
|
||||||
|
Regression test: image_generation tool should not be mutated between calls,
|
||||||
|
which would cause NotImplementedError on subsequent invocations.
|
||||||
|
"""
|
||||||
|
tools: list[dict[str, Any]] = [
|
||||||
|
{"type": "image_generation"},
|
||||||
|
{"type": "function", "name": "my_tool", "parameters": {}},
|
||||||
|
]
|
||||||
|
llm = ChatOpenAI(
|
||||||
|
model="gpt-4o",
|
||||||
|
use_responses_api=True,
|
||||||
|
streaming=True,
|
||||||
|
)
|
||||||
|
llm_with_tools = llm.bind_tools(tools)
|
||||||
|
|
||||||
|
mock_client = MagicMock()
|
||||||
|
|
||||||
|
def mock_create(*args: Any, **kwargs: Any) -> MockSyncContextManager:
|
||||||
|
return MockSyncContextManager(responses_stream)
|
||||||
|
|
||||||
|
mock_client.responses.create = mock_create
|
||||||
|
|
||||||
|
# First call should work
|
||||||
|
with patch.object(llm, "root_client", mock_client):
|
||||||
|
chunks = list(llm_with_tools.stream("test"))
|
||||||
|
assert len(chunks) > 0
|
||||||
|
|
||||||
|
# Second call should also work (would fail before fix due to tool mutation)
|
||||||
|
with patch.object(llm, "root_client", mock_client):
|
||||||
|
chunks = list(llm_with_tools.stream("test again"))
|
||||||
|
assert len(chunks) > 0
|
||||||
|
|||||||
Reference in New Issue
Block a user