mirror of
https://github.com/hwchase17/langchain.git
synced 2026-06-09 18:50:33 +00:00
openai[patch]: detect old models in with_structured_output (#29392)
Co-authored-by: ccurme <chester.curme@gmail.com>
This commit is contained in:
@@ -27,6 +27,7 @@ from langchain_tests.integration_tests.chat_models import (
|
||||
magic_function as invalid_magic_function,
|
||||
)
|
||||
from pydantic import BaseModel, Field
|
||||
from typing_extensions import TypedDict
|
||||
|
||||
from langchain_openai import ChatOpenAI
|
||||
from tests.unit_tests.fake.callbacks import FakeCallbackHandler
|
||||
@@ -1220,3 +1221,14 @@ def test_o1_doesnt_stream() -> None:
|
||||
def test_o1_stream_default_works() -> None:
|
||||
result = list(ChatOpenAI(model="o1").stream("say 'hi'"))
|
||||
assert len(result) > 0
|
||||
|
||||
|
||||
def test_structured_output_old_model() -> None:
|
||||
class Output(TypedDict):
|
||||
"""output."""
|
||||
|
||||
foo: str
|
||||
|
||||
llm = ChatOpenAI(model="gpt-4").with_structured_output(Output)
|
||||
output = llm.invoke("bar")
|
||||
assert "foo" in output
|
||||
|
||||
@@ -3,6 +3,9 @@
|
||||
import os
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
from typing_extensions import TypedDict
|
||||
|
||||
from langchain_openai import AzureChatOpenAI
|
||||
|
||||
|
||||
@@ -59,3 +62,22 @@ def test_initialize_azure_openai_with_openai_api_base_set() -> None:
|
||||
ls_params = llm._get_ls_params()
|
||||
assert ls_params["ls_provider"] == "azure"
|
||||
assert ls_params["ls_model_name"] == "35-turbo-dev"
|
||||
|
||||
|
||||
def test_structured_output_old_model() -> None:
|
||||
class Output(TypedDict):
|
||||
"""output."""
|
||||
|
||||
foo: str
|
||||
|
||||
with pytest.warns(match="Cannot use method='json_schema'"):
|
||||
llm = AzureChatOpenAI( # type: ignore[call-arg]
|
||||
model="gpt-35-turbo",
|
||||
azure_deployment="35-turbo-dev",
|
||||
openai_api_version="2023-05-15",
|
||||
azure_endpoint="my-base-url",
|
||||
).with_structured_output(Output)
|
||||
|
||||
# assert tool calling was used instead of json_schema
|
||||
assert "tools" in llm.steps[0].kwargs # type: ignore
|
||||
assert "response_format" not in llm.steps[0].kwargs # type: ignore
|
||||
|
||||
@@ -746,7 +746,7 @@ class Foo(BaseModel):
|
||||
def test_schema_from_with_structured_output(schema: Type) -> None:
|
||||
"""Test schema from with_structured_output."""
|
||||
|
||||
llm = ChatOpenAI()
|
||||
llm = ChatOpenAI(model="gpt-4o")
|
||||
|
||||
structured_llm = llm.with_structured_output(
|
||||
schema, method="json_schema", strict=True
|
||||
@@ -886,3 +886,16 @@ def test_init_o1() -> None:
|
||||
with pytest.warns(None) as record: # type: ignore[call-overload]
|
||||
ChatOpenAI(model="o1", reasoning_effort="medium")
|
||||
assert len(record) == 0
|
||||
|
||||
|
||||
def test_structured_output_old_model() -> None:
|
||||
class Output(TypedDict):
|
||||
"""output."""
|
||||
|
||||
foo: str
|
||||
|
||||
with pytest.warns(match="Cannot use method='json_schema'"):
|
||||
llm = ChatOpenAI(model="gpt-4").with_structured_output(Output)
|
||||
# assert tool calling was used instead of json_schema
|
||||
assert "tools" in llm.steps[0].kwargs # type: ignore
|
||||
assert "response_format" not in llm.steps[0].kwargs # type: ignore
|
||||
|
||||
Reference in New Issue
Block a user