Skip to content

Commit

Permalink
openai[patch]: integration test structured output (#19459)
Browse files Browse the repository at this point in the history
  • Loading branch information
efriis committed Mar 22, 2024
1 parent ac57123 commit a99e644
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"""Test AzureChatOpenAI wrapper."""

import os
from typing import Any, Optional

import pytest
from langchain_core.callbacks import CallbackManager
from langchain_core.messages import BaseMessage, BaseMessageChunk, HumanMessage
from langchain_core.outputs import ChatGeneration, ChatResult, LLMResult
from langchain_core.pydantic_v1 import BaseModel

from langchain_openai import AzureChatOpenAI
from tests.unit_tests.fake.callbacks import FakeCallbackHandler
Expand Down Expand Up @@ -223,3 +225,18 @@ def test_openai_invoke(llm: AzureChatOpenAI) -> None:

result = llm.invoke("I'm Pickle Rick", config=dict(tags=["foo"]))
assert isinstance(result.content, str)


@pytest.mark.skip(reason="Need tool calling model deployed on azure")
def test_openai_structured_output(llm: AzureChatOpenAI) -> None:
class MyModel(BaseModel):
"""A Person"""

name: str
age: int

llm_structure = llm.with_structured_output(MyModel)
result = llm_structure.invoke("I'm a 27 year old named Erick")
assert isinstance(result, MyModel)
assert result.name == "Erick"
assert result.age == 27
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test ChatOpenAI chat model."""

from typing import Any, Optional, cast

import pytest
Expand Down Expand Up @@ -467,3 +468,17 @@ async def test_async_response_metadata_streaming() -> None:
)
)
assert "content" in cast(BaseMessageChunk, full).response_metadata["logprobs"]


def test_openai_structured_output() -> None:
class MyModel(BaseModel):
"""A Person"""

name: str
age: int

llm = ChatOpenAI().with_structured_output(MyModel)
result = llm.invoke("I'm a 27 year old named Erick")
assert isinstance(result, MyModel)
assert result.name == "Erick"
assert result.age == 27

0 comments on commit a99e644

Please sign in to comment.