mirror of
https://github.com/hwchase17/langchain.git
synced 2026-06-09 10:17:00 +00:00
fix(openai): update test (#33659)
This commit is contained in:
@@ -18,6 +18,7 @@ from pydantic import BaseModel
|
|||||||
from typing_extensions import TypedDict
|
from typing_extensions import TypedDict
|
||||||
|
|
||||||
from langchain_openai import ChatOpenAI, custom_tool
|
from langchain_openai import ChatOpenAI, custom_tool
|
||||||
|
from langchain_openai.chat_models.base import _convert_to_openai_response_format
|
||||||
|
|
||||||
MODEL_NAME = "gpt-4o-mini"
|
MODEL_NAME = "gpt-4o-mini"
|
||||||
|
|
||||||
@@ -247,24 +248,30 @@ def test_parsed_dict_schema(schema: Any) -> None:
|
|||||||
def test_parsed_strict() -> None:
|
def test_parsed_strict() -> None:
|
||||||
llm = ChatOpenAI(model=MODEL_NAME, use_responses_api=True)
|
llm = ChatOpenAI(model=MODEL_NAME, use_responses_api=True)
|
||||||
|
|
||||||
class InvalidJoke(TypedDict):
|
class Joke(TypedDict):
|
||||||
setup: Annotated[str, ..., "The setup of the joke"]
|
setup: Annotated[str, ..., "The setup of the joke"]
|
||||||
punchline: Annotated[str, None, "The punchline of the joke"]
|
punchline: Annotated[str, None, "The punchline of the joke"]
|
||||||
|
|
||||||
|
schema = _convert_to_openai_response_format(Joke)
|
||||||
|
invalid_schema = cast(dict, _convert_to_openai_response_format(Joke, strict=True))
|
||||||
|
invalid_schema["json_schema"]["schema"]["required"] = ["setup"] # make invalid
|
||||||
|
|
||||||
# Test not strict
|
# Test not strict
|
||||||
response = llm.invoke("Tell me a joke", response_format=InvalidJoke)
|
response = llm.invoke("Tell me a joke", response_format=schema)
|
||||||
parsed = json.loads(response.text)
|
parsed = json.loads(response.text)
|
||||||
assert parsed == response.additional_kwargs["parsed"]
|
assert parsed == response.additional_kwargs["parsed"]
|
||||||
|
|
||||||
# Test strict
|
# Test strict
|
||||||
with pytest.raises(openai.BadRequestError):
|
with pytest.raises(openai.BadRequestError):
|
||||||
llm.invoke(
|
llm.invoke(
|
||||||
"Tell me a joke about cats.", response_format=InvalidJoke, strict=True
|
"Tell me a joke about cats.", response_format=invalid_schema, strict=True
|
||||||
)
|
)
|
||||||
with pytest.raises(openai.BadRequestError):
|
with pytest.raises(openai.BadRequestError):
|
||||||
next(
|
next(
|
||||||
llm.stream(
|
llm.stream(
|
||||||
"Tell me a joke about cats.", response_format=InvalidJoke, strict=True
|
"Tell me a joke about cats.",
|
||||||
|
response_format=invalid_schema,
|
||||||
|
strict=True,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user