From 2f0e3c66d1102aab3bb0f68273c456f08335d650 Mon Sep 17 00:00:00 2001 From: Vadym Barda Date: Mon, 16 Dec 2024 09:51:12 -0500 Subject: [PATCH] docs: update structured output how-to guide (#2773) Fixes #2760 --- .../how-tos/react-agent-structured-output.ipynb | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/docs/docs/how-tos/react-agent-structured-output.ipynb b/docs/docs/how-tos/react-agent-structured-output.ipynb index b87ece382..7379c9eb8 100644 --- a/docs/docs/how-tos/react-agent-structured-output.ipynb +++ b/docs/docs/how-tos/react-agent-structured-output.ipynb @@ -225,9 +225,19 @@ "# Define the function that responds to the user\n", "def respond(state: AgentState):\n", " # Construct the final answer from the arguments of the last tool call\n", - " response = WeatherResponse(**state[\"messages\"][-1].tool_calls[0][\"args\"])\n", + " weather_tool_call = state[\"messages\"][-1].tool_calls[0]\n", + " response = WeatherResponse(**weather_tool_call[\"args\"])\n", + " # Since we're using tool calling to return structured output,\n", + " # we need to add a tool message corresponding to the WeatherResponse tool call,\n", + " # This is due to LLM providers' requirement that AI messages with tool calls\n", + " # need to be followed by a tool message for each tool call\n", + " tool_message = {\n", + " \"type\": \"tool\",\n", + " \"content\": \"Here is your structured response\",\n", + " \"tool_call_id\": weather_tool_call[\"id\"],\n", + " }\n", " # We return the final answer\n", - " return {\"final_response\": response}\n", + " return {\"final_response\": response, \"messages\": [tool_message]}\n", "\n", "\n", "# Define the function that determines whether to continue or not\n", @@ -466,7 +476,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.4" + "version": "3.12.3" } }, "nbformat": 4,