{ "cells": [ { "cell_type": "markdown", "id": "51466c8d-8ce4-4b3d-be4e-18fdbeda5f53", "metadata": {}, "source": [ "# How to stream values from your graph\n", "\n", "There are multiple different streaming modes.\n", "\n", "- `values`: This streaming mode streams back values of the graph. This is the **full state of the graph** after each node is called.\n", "- `updates`: This streaming mode streams back updates to the graph. This is the **update to the state of the graph** after each node is called.\n", "- `messages`: This streaming mode streams back messages - both complete messages (at the end of a node) as well as **tokens** for any messages generated inside a node. This mode is primarily meant for powering chat applications.\n", "\n", "\n", "This notebook covers `streaming_mode=\"values\"`." ] }, { "cell_type": "code", "execution_count": 1, "id": "521d975b-e94b-4c37-bfa1-82d969e2a4dc", "metadata": {}, "outputs": [], "source": [ "from langgraph_sdk import get_client" ] }, { "cell_type": "code", "execution_count": 2, "id": "27a1392b-86c3-464e-99a8-90ffc965f3ec", "metadata": {}, "outputs": [], "source": [ "client = get_client()" ] }, { "cell_type": "code", "execution_count": 3, "id": "4947e9bc-111f-4991-8c41-1041da9bf0ba", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca',\n", " 'graph_id': 'agent',\n", " 'config': {},\n", " 'created_at': '2024-05-18T00:19:39.688822+00:00',\n", " 'updated_at': '2024-05-18T00:19:39.688822+00:00',\n", " 'metadata': {'created_by': 'system'}}]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# There should always be a default assistant with no configuration\n", "assistants = await client.assistants.search()\n", "assistants = [a for a in assistants if not a['config']]\n", "assistants" ] }, { "cell_type": "code", "execution_count": 4, "id": "230c0464-a6e5-420f-9e38-ca514e5634ce", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'assistant_id': 'fe096781-5601-53d2-b2f6-0d3403f7e9ca',\n", " 'graph_id': 'agent',\n", " 'config': {},\n", " 'created_at': '2024-05-18T00:19:39.688822+00:00',\n", " 'updated_at': '2024-05-18T00:19:39.688822+00:00',\n", " 'metadata': {'created_by': 'system'}}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "assistant = assistants[0]\n", "assistant" ] }, { "cell_type": "code", "execution_count": 7, "id": "7da70e20-1a4e-4df2-b996-1927f474c835", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Receiving new event of type: metadata...\n", "{'run_id': 'f08791ce-0a3d-44e0-836c-ff62cd2e2786'}\n", "\n", "\n", "\n", "Receiving new event of type: values...\n", "{'messages': [{'role': 'human', 'content': 'whats the weather in la'}]}\n", "\n", "\n", "\n", "Receiving new event of type: values...\n", "{'messages': [{'content': 'whats the weather in la', 'additional_kwargs': {}, 'response_metadata': {}, 'type': 'human', 'name': None, 'id': 'faa15565-8823-4aa1-87af-e21b40526fae', 'example': False}, {'content': [{'id': 'toolu_01E5mSaZWm5rWJnCqmt63v4g', 'input': {'query': 'weather in los angeles'}, 'name': 'tavily_search_results_json', 'type': 'tool_use'}], 'additional_kwargs': {}, 'response_metadata': {}, 'type': 'ai', 'name': None, 'id': 'run-3fe1db7a-6b8d-4d83-ba07-8657190ad811', 'example': False, 'tool_calls': [{'name': 'tavily_search_results_json', 'args': {'query': 'weather in los angeles'}, 'id': 'toolu_01E5mSaZWm5rWJnCqmt63v4g'}], 'invalid_tool_calls': []}]}\n", "\n", "\n", "\n", "Receiving new event of type: values...\n", "{'messages': [{'content': 'whats the weather in la', 'additional_kwargs': {}, 'response_metadata': {}, 'type': 'human', 'name': None, 'id': 'faa15565-8823-4aa1-87af-e21b40526fae', 'example': False}, {'content': [{'id': 'toolu_01E5mSaZWm5rWJnCqmt63v4g', 'input': {'query': 'weather in los angeles'}, 'name': 'tavily_search_results_json', 'type': 'tool_use'}], 'additional_kwargs': {}, 'response_metadata': {}, 'type': 'ai', 'name': None, 'id': 'run-3fe1db7a-6b8d-4d83-ba07-8657190ad811', 'example': False, 'tool_calls': [{'name': 'tavily_search_results_json', 'args': {'query': 'weather in los angeles'}, 'id': 'toolu_01E5mSaZWm5rWJnCqmt63v4g'}], 'invalid_tool_calls': []}, {'content': '[{\"url\": \"https://www.weatherapi.com/\", \"content\": \"{\\'location\\': {\\'name\\': \\'Los Angeles\\', \\'region\\': \\'California\\', \\'country\\': \\'United States of America\\', \\'lat\\': 34.05, \\'lon\\': -118.24, \\'tz_id\\': \\'America/Los_Angeles\\', \\'localtime_epoch\\': 1716310320, \\'localtime\\': \\'2024-05-21 9:52\\'}, \\'current\\': {\\'last_updated_epoch\\': 1716309900, \\'last_updated\\': \\'2024-05-21 09:45\\', \\'temp_c\\': 16.7, \\'temp_f\\': 62.1, \\'is_day\\': 1, \\'condition\\': {\\'text\\': \\'Overcast\\', \\'icon\\': \\'//cdn.weatherapi.com/weather/64x64/day/122.png\\', \\'code\\': 1009}, \\'wind_mph\\': 8.1, \\'wind_kph\\': 13.0, \\'wind_degree\\': 250, \\'wind_dir\\': \\'WSW\\', \\'pressure_mb\\': 1015.0, \\'pressure_in\\': 29.97, \\'precip_mm\\': 0.0, \\'precip_in\\': 0.0, \\'humidity\\': 65, \\'cloud\\': 100, \\'feelslike_c\\': 16.7, \\'feelslike_f\\': 62.1, \\'vis_km\\': 16.0, \\'vis_miles\\': 9.0, \\'uv\\': 5.0, \\'gust_mph\\': 12.5, \\'gust_kph\\': 20.2}}\"}]', 'additional_kwargs': {}, 'response_metadata': {}, 'type': 'tool', 'name': 'tavily_search_results_json', 'id': '0d5dab31-5ff8-4ae2-a560-bc4bcba7c9d7', 'tool_call_id': 'toolu_01E5mSaZWm5rWJnCqmt63v4g'}]}\n", "\n", "\n", "\n", "Receiving new event of type: values...\n", "{'messages': [{'content': 'whats the weather in la', 'additional_kwargs': {}, 'response_metadata': {}, 'type': 'human', 'name': None, 'id': 'faa15565-8823-4aa1-87af-e21b40526fae', 'example': False}, {'content': [{'id': 'toolu_01E5mSaZWm5rWJnCqmt63v4g', 'input': {'query': 'weather in los angeles'}, 'name': 'tavily_search_results_json', 'type': 'tool_use'}], 'additional_kwargs': {}, 'response_metadata': {}, 'type': 'ai', 'name': None, 'id': 'run-3fe1db7a-6b8d-4d83-ba07-8657190ad811', 'example': False, 'tool_calls': [{'name': 'tavily_search_results_json', 'args': {'query': 'weather in los angeles'}, 'id': 'toolu_01E5mSaZWm5rWJnCqmt63v4g'}], 'invalid_tool_calls': []}, {'content': '[{\"url\": \"https://www.weatherapi.com/\", \"content\": \"{\\'location\\': {\\'name\\': \\'Los Angeles\\', \\'region\\': \\'California\\', \\'country\\': \\'United States of America\\', \\'lat\\': 34.05, \\'lon\\': -118.24, \\'tz_id\\': \\'America/Los_Angeles\\', \\'localtime_epoch\\': 1716310320, \\'localtime\\': \\'2024-05-21 9:52\\'}, \\'current\\': {\\'last_updated_epoch\\': 1716309900, \\'last_updated\\': \\'2024-05-21 09:45\\', \\'temp_c\\': 16.7, \\'temp_f\\': 62.1, \\'is_day\\': 1, \\'condition\\': {\\'text\\': \\'Overcast\\', \\'icon\\': \\'//cdn.weatherapi.com/weather/64x64/day/122.png\\', \\'code\\': 1009}, \\'wind_mph\\': 8.1, \\'wind_kph\\': 13.0, \\'wind_degree\\': 250, \\'wind_dir\\': \\'WSW\\', \\'pressure_mb\\': 1015.0, \\'pressure_in\\': 29.97, \\'precip_mm\\': 0.0, \\'precip_in\\': 0.0, \\'humidity\\': 65, \\'cloud\\': 100, \\'feelslike_c\\': 16.7, \\'feelslike_f\\': 62.1, \\'vis_km\\': 16.0, \\'vis_miles\\': 9.0, \\'uv\\': 5.0, \\'gust_mph\\': 12.5, \\'gust_kph\\': 20.2}}\"}]', 'additional_kwargs': {}, 'response_metadata': {}, 'type': 'tool', 'name': 'tavily_search_results_json', 'id': '0d5dab31-5ff8-4ae2-a560-bc4bcba7c9d7', 'tool_call_id': 'toolu_01E5mSaZWm5rWJnCqmt63v4g'}, {'content': 'Based on the weather API results, the current weather in Los Angeles is overcast with a temperature of around 62°F (17°C). There are light winds from the west-southwest around 8-13 mph. The humidity is 65% and visibility is good at 9 miles. Overall, mild spring weather conditions in LA.', 'additional_kwargs': {}, 'response_metadata': {}, 'type': 'ai', 'name': None, 'id': 'run-4d6d4c23-5aad-4042-b0d9-19407a9e08e3', 'example': False, 'tool_calls': [], 'invalid_tool_calls': []}]}\n", "\n", "\n", "\n", "Receiving new event of type: end...\n", "None\n", "\n", "\n", "\n" ] } ], "source": [ "input = {\"messages\": [{\"role\": \"human\", \"content\": \"whats the weather in la\"}]}\n", "thread = await client.threads.create()\n", "async for chunk in client.runs.stream(thread['thread_id'], assistant['assistant_id'], input=input):\n", " print(f\"Receiving new event of type: {chunk.event}...\")\n", " print(chunk.data)\n", " print(\"\\n\\n\")" ] }, { "cell_type": "markdown", "id": "43e4432d-e96c-4ae4-8085-866fb57bbcb3", "metadata": {}, "source": [ "If we want to just get the final result, we can use this endpoint and just keep track of the last value we received" ] }, { "cell_type": "code", "execution_count": 8, "id": "d2560481-d161-4d4f-b385-4977696c4aa1", "metadata": {}, "outputs": [], "source": [ "input = {\"messages\": [{\"role\": \"human\", \"content\": \"whats the weather in la\"}]}\n", "thread = await client.threads.create()\n", "final_answer = None\n", "async for chunk in client.runs.stream(thread['thread_id'], assistant['assistant_id'], input=input):\n", " if chunk.event == \"values\":\n", " final_answer = chunk.data" ] }, { "cell_type": "code", "execution_count": 9, "id": "9c2d60ea-450f-45cd-b867-0cbb162528f6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'messages': [{'content': 'whats the weather in la',\n", " 'additional_kwargs': {},\n", " 'response_metadata': {},\n", " 'type': 'human',\n", " 'name': None,\n", " 'id': 'e78c2f94-d810-42fc-a399-11f6bb1b1092',\n", " 'example': False},\n", " {'content': [{'id': 'toolu_01SBMoAGr4U9x3ibztm2UUom',\n", " 'input': {'query': 'weather in los angeles'},\n", " 'name': 'tavily_search_results_json',\n", " 'type': 'tool_use'}],\n", " 'additional_kwargs': {},\n", " 'response_metadata': {},\n", " 'type': 'ai',\n", " 'name': None,\n", " 'id': 'run-80767ab8-09fc-40ec-9e45-657ddef5e0b1',\n", " 'example': False,\n", " 'tool_calls': [{'name': 'tavily_search_results_json',\n", " 'args': {'query': 'weather in los angeles'},\n", " 'id': 'toolu_01SBMoAGr4U9x3ibztm2UUom'}],\n", " 'invalid_tool_calls': []},\n", " {'content': '[{\"url\": \"https://www.weatherapi.com/\", \"content\": \"{\\'location\\': {\\'name\\': \\'Los Angeles\\', \\'region\\': \\'California\\', \\'country\\': \\'United States of America\\', \\'lat\\': 34.05, \\'lon\\': -118.24, \\'tz_id\\': \\'America/Los_Angeles\\', \\'localtime_epoch\\': 1716310320, \\'localtime\\': \\'2024-05-21 9:52\\'}, \\'current\\': {\\'last_updated_epoch\\': 1716309900, \\'last_updated\\': \\'2024-05-21 09:45\\', \\'temp_c\\': 16.7, \\'temp_f\\': 62.1, \\'is_day\\': 1, \\'condition\\': {\\'text\\': \\'Overcast\\', \\'icon\\': \\'//cdn.weatherapi.com/weather/64x64/day/122.png\\', \\'code\\': 1009}, \\'wind_mph\\': 8.1, \\'wind_kph\\': 13.0, \\'wind_degree\\': 250, \\'wind_dir\\': \\'WSW\\', \\'pressure_mb\\': 1015.0, \\'pressure_in\\': 29.97, \\'precip_mm\\': 0.0, \\'precip_in\\': 0.0, \\'humidity\\': 65, \\'cloud\\': 100, \\'feelslike_c\\': 16.7, \\'feelslike_f\\': 62.1, \\'vis_km\\': 16.0, \\'vis_miles\\': 9.0, \\'uv\\': 5.0, \\'gust_mph\\': 12.5, \\'gust_kph\\': 20.2}}\"}]',\n", " 'additional_kwargs': {},\n", " 'response_metadata': {},\n", " 'type': 'tool',\n", " 'name': 'tavily_search_results_json',\n", " 'id': 'af25e94a-c119-48c3-bbd3-096e42f472ac',\n", " 'tool_call_id': 'toolu_01SBMoAGr4U9x3ibztm2UUom'},\n", " {'content': 'Based on the weather API results, the current weather in Los Angeles is overcast with a temperature of around 62°F (17°C). There are light winds from the west-southwest around 8-13 mph. The humidity is 65% and visibility is good at 9 miles. Overall, mild spring weather conditions in LA.',\n", " 'additional_kwargs': {},\n", " 'response_metadata': {},\n", " 'type': 'ai',\n", " 'name': None,\n", " 'id': 'run-b90f0037-e56a-4f3b-ad92-00d10d079a9e',\n", " 'example': False,\n", " 'tool_calls': [],\n", " 'invalid_tool_calls': []}]}" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "final_answer" ] }, { "cell_type": "code", "execution_count": null, "id": "39cedbff-0a7f-4a3e-bfc1-595797358769", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.1" } }, "nbformat": 4, "nbformat_minor": 5 }