mirror of
https://github.com/hwchase17/langchain.git
synced 2026-06-09 18:50:33 +00:00
openai[patch]: support structured output and tools (#30581)
Co-authored-by: ccurme <chester.curme@gmail.com>
This commit is contained in:
@@ -1265,3 +1265,38 @@ def test_structured_output_and_tools() -> None:
|
||||
assert len(full.tool_calls) == 1
|
||||
tool_call = full.tool_calls[0]
|
||||
assert tool_call["name"] == "GenerateUsername"
|
||||
|
||||
|
||||
def test_tools_and_structured_output() -> None:
|
||||
class ResponseFormat(BaseModel):
|
||||
response: str
|
||||
explanation: str
|
||||
|
||||
llm = ChatOpenAI(model="gpt-4o-mini").with_structured_output(
|
||||
ResponseFormat, strict=True, include_raw=True, tools=[GenerateUsername]
|
||||
)
|
||||
|
||||
expected_keys = {"raw", "parsing_error", "parsed"}
|
||||
query = "Hello"
|
||||
tool_query = "Generate a user name for Alice, black hair. Use the tool."
|
||||
# Test invoke
|
||||
## Engage structured output
|
||||
response = llm.invoke(query)
|
||||
assert isinstance(response["parsed"], ResponseFormat)
|
||||
## Engage tool calling
|
||||
response_tools = llm.invoke(tool_query)
|
||||
ai_msg = response_tools["raw"]
|
||||
assert isinstance(ai_msg, AIMessage)
|
||||
assert ai_msg.tool_calls
|
||||
assert response_tools["parsed"] is None
|
||||
|
||||
# Test stream
|
||||
aggregated: dict = {}
|
||||
for chunk in llm.stream(tool_query):
|
||||
assert isinstance(chunk, dict)
|
||||
assert all(key in expected_keys for key in chunk)
|
||||
aggregated = {**aggregated, **chunk}
|
||||
assert all(key in aggregated for key in expected_keys)
|
||||
assert isinstance(aggregated["raw"], AIMessage)
|
||||
assert aggregated["raw"].tool_calls
|
||||
assert aggregated["parsed"] is None
|
||||
|
||||
Reference in New Issue
Block a user