openai[patch]: support structured output and tools (#30581)

Co-authored-by: ccurme <chester.curme@gmail.com>
This commit is contained in:
Bagatur
2025-04-02 06:14:02 -07:00
committed by GitHub
parent 32f7695809
commit 111dd90a46
2 changed files with 93 additions and 2 deletions

View File

@@ -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