mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-18 12:58:59 +00:00
Merge branch 'master' into bagatur/coerce_input
This commit is contained in:
commit
770031de82
@ -2146,9 +2146,13 @@ def _convert_to_openai_response_format(
|
|||||||
if isinstance(schema, type) and is_basemodel_subclass(schema):
|
if isinstance(schema, type) and is_basemodel_subclass(schema):
|
||||||
return schema
|
return schema
|
||||||
|
|
||||||
if "json_schema" in schema and schema.get("type") == "json_schema":
|
if (
|
||||||
|
isinstance(schema, dict)
|
||||||
|
and "json_schema" in schema
|
||||||
|
and schema.get("type") == "json_schema"
|
||||||
|
):
|
||||||
response_format = schema
|
response_format = schema
|
||||||
elif "name" in schema and "schema" in schema:
|
elif isinstance(schema, dict) and "name" in schema and "schema" in schema:
|
||||||
response_format = {"type": "json_schema", "json_schema": schema}
|
response_format = {"type": "json_schema", "json_schema": schema}
|
||||||
else:
|
else:
|
||||||
strict = strict if strict is not None else True
|
strict = strict if strict is not None else True
|
||||||
|
@ -17,7 +17,8 @@ from langchain_core.messages import (
|
|||||||
ToolMessage,
|
ToolMessage,
|
||||||
)
|
)
|
||||||
from langchain_core.messages.ai import UsageMetadata
|
from langchain_core.messages.ai import UsageMetadata
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel, Field
|
||||||
|
from typing_extensions import TypedDict
|
||||||
|
|
||||||
from langchain_openai import ChatOpenAI
|
from langchain_openai import ChatOpenAI
|
||||||
from langchain_openai.chat_models.base import (
|
from langchain_openai.chat_models.base import (
|
||||||
@ -805,3 +806,42 @@ def test__convert_to_openai_response_format() -> None:
|
|||||||
|
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
_convert_to_openai_response_format(response_format, strict=False)
|
_convert_to_openai_response_format(response_format, strict=False)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("method", ["function_calling", "json_schema"])
|
||||||
|
@pytest.mark.parametrize("strict", [True, None])
|
||||||
|
def test_structured_output_strict(
|
||||||
|
method: Literal["function_calling", "json_schema"], strict: Optional[bool]
|
||||||
|
) -> None:
|
||||||
|
"""Test to verify structured output with strict=True."""
|
||||||
|
|
||||||
|
llm = ChatOpenAI(model="gpt-4o-2024-08-06")
|
||||||
|
|
||||||
|
class Joke(BaseModel):
|
||||||
|
"""Joke to tell user."""
|
||||||
|
|
||||||
|
setup: str = Field(description="question to set up a joke")
|
||||||
|
punchline: str = Field(description="answer to resolve the joke")
|
||||||
|
|
||||||
|
llm.with_structured_output(Joke, method=method, strict=strict)
|
||||||
|
# Schema
|
||||||
|
llm.with_structured_output(Joke.model_json_schema(), method=method, strict=strict)
|
||||||
|
|
||||||
|
|
||||||
|
def test_nested_structured_output_strict() -> None:
|
||||||
|
"""Test to verify structured output with strict=True for nested object."""
|
||||||
|
|
||||||
|
llm = ChatOpenAI(model="gpt-4o-2024-08-06")
|
||||||
|
|
||||||
|
class SelfEvaluation(TypedDict):
|
||||||
|
score: int
|
||||||
|
text: str
|
||||||
|
|
||||||
|
class JokeWithEvaluation(TypedDict):
|
||||||
|
"""Joke to tell user."""
|
||||||
|
|
||||||
|
setup: str
|
||||||
|
punchline: str
|
||||||
|
self_evaluation: SelfEvaluation
|
||||||
|
|
||||||
|
llm.with_structured_output(JokeWithEvaluation, method="json_schema")
|
||||||
|
Loading…
Reference in New Issue
Block a user