From d82eec6aadc44ed61f613d928260c12ebc1cba77 Mon Sep 17 00:00:00 2001 From: ccurme Date: Thu, 12 Sep 2024 09:39:51 -0400 Subject: [PATCH] standard-tests[patch]: add standard test for structured output with optional param (#26384) --- .../integration_tests/chat_models.py | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/libs/standard-tests/langchain_standard_tests/integration_tests/chat_models.py b/libs/standard-tests/langchain_standard_tests/integration_tests/chat_models.py index 171b1936a2c..805cf57549a 100644 --- a/libs/standard-tests/langchain_standard_tests/integration_tests/chat_models.py +++ b/libs/standard-tests/langchain_standard_tests/integration_tests/chat_models.py @@ -302,6 +302,28 @@ class ChatModelIntegrationTests(ChatModelTests): assert isinstance(chunk, dict) # for mypy assert set(chunk.keys()) == {"setup", "punchline"} + def test_structured_output_optional_param(self, model: BaseChatModel) -> None: + """Test to verify structured output with an optional param.""" + if not self.has_tool_calling: + pytest.skip("Test requires tool calling.") + + class Joke(BaseModel): + """Joke to tell user.""" + + setup: str = Field(description="question to set up a joke") + punchline: Optional[str] = Field( + default=None, description="answer to resolve the joke" + ) + + chat = model.with_structured_output(Joke) # type: ignore[arg-type] + setup_result = chat.invoke( + "Give me the setup to a joke about cats, no punchline." + ) + assert isinstance(setup_result, Joke) + + joke_result = chat.invoke("Give me a joke about cats, include the punchline.") + assert isinstance(joke_result, Joke) + def test_tool_message_histories_string_content(self, model: BaseChatModel) -> None: """ Test that message histories are compatible with string tool contents