Skip to content

Commit 79c64d1

Browse files
author
Louis Auneau
committed
fix(openai): avoid duplicated error check in with_structured_output with raw input
1 parent f01a368 commit 79c64d1

File tree

2 files changed

+7
-2
lines changed
  • libs/partners/openai

2 files changed

+7
-2
lines changed

libs/partners/openai/langchain_openai/chat_models/base.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult
9090
from langchain_core.runnables import (
9191
Runnable,
92+
RunnableBranch,
9293
RunnableLambda,
9394
RunnableMap,
9495
RunnablePassthrough,
@@ -2215,8 +2216,12 @@ def _get_raw_structured_output_chain(
22152216
parser_with_fallback = parser_assign.with_fallbacks(
22162217
[parser_error_fallback], exception_key="parsing_error"
22172218
)
2219+
branch = RunnableBranch[dict, dict](
2220+
(lambda x: x.get("parsing_error") is not None, RunnablePassthrough()),
2221+
parser_with_fallback,
2222+
)
22182223

2219-
return wrap_input | llm_with_fallback | parser_with_fallback
2224+
return wrap_input | llm_with_fallback | branch
22202225

22212226
def _get_message_from_error_response(
22222227
self, error: Exception

libs/partners/openai/tests/unit_tests/chat_models/test_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3266,7 +3266,7 @@ def test_with_structured_output_validation_error() -> None:
32663266
"usage": {"prompt_tokens": 10, "completion_tokens": 5, "total_tokens": 15},
32673267
}
32683268
mock_api_response = MagicMock(spec=APIResponse)
3269-
mock_api_response.http_response = MagicMock(spec=Response)
3269+
mock_api_response.http_response = MagicMock(spec=httpx.Response)
32703270
mock_api_response.http_response.json.return_value = response_json
32713271
mock_api_response.parse.side_effect = ValidationError.from_exception_data(
32723272
title="Foo",

0 commit comments

Comments
 (0)