Skip to content

Commit

Permalink
added a how-to for new retry dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaZelenskis committed Jan 8, 2025
1 parent 662a986 commit 160ee8c
Showing 1 changed file with 67 additions and 16 deletions.
83 changes: 67 additions & 16 deletions docs/docs/how-tos/node-retries.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -41,7 +41,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -78,20 +78,9 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"RetryPolicy(initial_interval=0.5, backoff_factor=2.0, max_interval=128.0, max_attempts=3, jitter=True, retry_on=<function default_retry_on at 0x78b964b89940>)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"from langgraph.pregel import RetryPolicy\n",
"\n",
Expand Down Expand Up @@ -131,7 +120,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -180,6 +169,68 @@
"\n",
"graph = builder.compile()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## State-Modifying Retry Policies\n",
"\n",
"Sometimes you might want to modify the state before retrying after an error occurs. LangGraph now supports this through a dictionary mapping of exceptions to state-modifying functions in the retry policy.\n"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Exception occured: Error occurred in processing\n",
"Counter: 0\n",
"Exception occured: Error occurred in processing\n",
"Counter: 1\n",
"Exception occured: Error occurred in processing\n",
"Counter: 2\n",
"{'counter': 3, 'result': 'Success!'}\n"
]
}
],
"source": [
"class CounterState(TypedDict):\n",
" counter: int\n",
" result: str\n",
"\n",
"\n",
"def increment_counter(state: CounterState, exception: Exception):\n",
" print(f\"Exception occured: {exception}\")\n",
" print(f\"Counter: {state['counter']}\")\n",
" state['counter'] += 1\n",
"\n",
"def processing_node(state: CounterState) -> CounterState:\n",
" if state['counter'] < 3:\n",
" raise ValueError(\"Error occurred in processing\")\n",
" state['result'] = \"Success!\"\n",
" return state\n",
"\n",
"workflow = StateGraph(CounterState)\n",
"\n",
"retry_policy = RetryPolicy(\n",
" max_attempts=5,\n",
" retry_on={ValueError: increment_counter}\n",
")\n",
"\n",
"\n",
"workflow.add_node(\"process\", processing_node, retry=retry_policy)\n",
"workflow.add_edge(START, \"process\")\n",
"workflow.add_edge(\"process\", END)\n",
"\n",
"app = workflow.compile()\n",
"final_state = app.invoke({\"counter\": 0, \"result\": \"\"})\n",
"print(final_state)"
]
}
],
"metadata": {
Expand Down

0 comments on commit 160ee8c

Please sign in to comment.