The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
docs: add example of simultaneous tool-calling + structured output for OpenAI (#31433)
ccurmebbb60e210a09
diff --git a/docs/docs/integrations/chat/openai.ipynb b/docs/docs/integrations/chat/openai.ipynbindex 8df8e6d0c1..58bec4b2f6 100644--- a/docs/docs/integrations/chat/openai.ipynb+++ b/docs/docs/integrations/chat/openai.ipynb@@ -397,6 +397,56 @@"For more on binding tools and tool call outputs, head to the [tool calling](/docs/how_to/function_calling) docs."]},+ {+ "cell_type": "markdown",+ "id": "f06789fb-61e1-4b35-a2b5-2dea18c1a949",+ "metadata": {},+ "source": [+ "### Structured output and tool calls\n",+ "\n",+ "OpenAI's [structured output](https://platform.openai.com/docs/guides/structured-outputs) feature can be used simultaneously with tool-calling. The model will either generate tool calls or a response adhering to a desired schema. See example below:"+ ]+ },+ {+ "cell_type": "code",+ "execution_count": null,+ "id": "15d2b6e0-f457-4abd-a4d5-08b210d09c04",+ "metadata": {},+ "outputs": [],+ "source": [+ "from langchain_openai import ChatOpenAI\n",+ "from pydantic import BaseModel\n",+ "\n",+ "\n",+ "def get_weather(location: str) -> None:\n",+ " \"\"\"Get weather at a location.\"\"\"\n",+ " return \"It's sunny.\"\n",+ "\n",+ "\n",+ "class OutputSchema(BaseModel):\n",+ " \"\"\"Schema for response.\"\"\"\n",+ "\n",+ " answer: str\n",+ " justification: str\n",+ "\n",+ "\n",+ "llm = ChatOpenAI(model=\"gpt-4.1\")\n",+ "\n",+ "structured_llm = llm.bind_tools(\n",+ " [get_weather],\n",+ " response_format=OutputSchema,\n",+ " strict=True,\n",+ ")\n",+ "\n",+ "# Response contains tool calls:\n",+ "tool_call_response = structured_llm.invoke(\"What is the weather in SF?\")\n",+ "\n",+ "# structured_response.additional_kwargs[\"parsed\"] contains parsed output\n",+ "structured_response = structured_llm.invoke(\n",+ " \"What weighs more, a pound of feathers or a pound of gold?\"\n",+ ")"+ ]+ },{"cell_type": "markdown","id": "84833dd0-17e9-4269-82ed-550639d65751",