Skip to content

Commit 011734e

Browse files
martin-martinclaudebzaczynski
authored
Update sample code for: LangGraph Tutorial: Build Stateful AI Agents in Python (#784)
* Update sample code for: LangGraph Tutorial: Build Stateful AI Agents in Python Refresh the LangGraph companion code for the tutorial update: - Bump to langgraph 1.2.5, langchain-openai 1.3.2, langchain-core 1.4.7, pydantic 2.13.4 (regenerated requirements.txt; pyproject floors raised) - Switch the model to gpt-5.4-nano - Fix check_escalation_status_node to fall back to 0 when no fine is extracted (avoids a TypeError on notices without a max potential fine) - Store the boolean answer.is_true in follow_ups (was storing the model object) - Drop the unused critical_fields_missing key from the agent's initial state - Point the README API-key link at platform.openai.com/api-keys and match the new tutorial title Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * Sync example_emails.py dates to 2026 to match the tutorial The companion emails still used 2024/2025 dates while the tutorial's examples and REPL transcripts use 2026. Update Email 0 (May 15 / June 10, 2026) and Email 3 (June 10 / August 5, 2026) so extracted dates match the tutorial output. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: Bartosz Zaczyński <bartosz.zaczynski@gmail.com>
1 parent da38d7d commit 011734e

9 files changed

Lines changed: 64 additions & 35 deletions

File tree

python-langgraph/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# LangGraph: Build Stateful AI Agents in Python
1+
# LangGraph Tutorial: Build Stateful AI Agents in Python
22

3-
This folder contains the source code for [LangGraph: Build Stateful AI Agents in Python](https://realpython.com/langgraph-python/)
3+
This folder contains the source code for [LangGraph Tutorial: Build Stateful AI Agents in Python](https://realpython.com/langgraph-python/)
44

55
## Setup
66

@@ -12,7 +12,7 @@ Create a new virtual environment, and run the following command to install LangG
1212

1313
You'll use `langchain-openai` to interact with OpenAI LLMs, but keep in mind you can use any LLM provider you like with LangGraph and LangChain. You'll use [`pydantic`](https://realpython.com/python-pydantic/) to validate the information your agent parses from emails.
1414

15-
Before moving forward, if you choose to use OpenAI, make sure you're signed up for an OpenAI account and you have a valid [API key](https://openai.com/api/). You'll need to set the following [environment variable](https://en.wikipedia.org/wiki/Environment_variable) before running any examples in this tutorial:
15+
Before moving forward, if you choose to use OpenAI, make sure you're signed up for an OpenAI account and you have a valid [API key](https://platform.openai.com/api-keys). You'll need to set the following [environment variable](https://en.wikipedia.org/wiki/Environment_variable) before running any examples in this tutorial:
1616

1717
```dotenv
1818
OPENAI_API_KEY=<YOUR-OPENAI-API-KEY>

python-langgraph/chains/binary_questions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class BinaryAnswer(BaseModel):
2323
]
2424
)
2525

26-
binary_question_model = ChatOpenAI(model="gpt-4o-mini", temperature=0)
26+
binary_question_model = ChatOpenAI(model="gpt-5.4-nano", temperature=0)
2727

2828
BINARY_QUESTION_CHAIN = (
2929
binary_question_prompt

python-langgraph/chains/escalation_check.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class EscalationCheck(BaseModel):
2525
]
2626
)
2727

28-
escalation_check_model = ChatOpenAI(model="gpt-4o-mini", temperature=0)
28+
escalation_check_model = ChatOpenAI(model="gpt-5.4-nano", temperature=0)
2929

3030
ESCALATION_CHECK_CHAIN = (
3131
escalation_prompt

python-langgraph/chains/notice_extraction.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def compliance_deadline(self) -> date | None:
9898
]
9999
)
100100

101-
notice_parser_model = ChatOpenAI(model="gpt-4o-mini", temperature=0)
101+
notice_parser_model = ChatOpenAI(model="gpt-5.4-nano", temperature=0)
102102

103103
NOTICE_PARSER_CHAIN = (
104104
info_parse_prompt

python-langgraph/example_emails.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
EMAILS = [
22
# Email 1
33
"""
4-
Date: October 15, 2024
4+
Date: May 15, 2026
55
From: Occupational Safety and Health Administration (OSHA)
66
To: Blue Ridge Construction, project 111232345 - Downtown Office Complex
77
Location: Dallas, TX
@@ -21,7 +21,7 @@
2121
Conduct an inspection of all scaffolding structures and reinforce unstable
2222
sections. Ensure all workers on-site are provided with necessary PPE and conduct
2323
safety training on proper usage.
24-
Deadline for Compliance: All violations must be rectified by November 10, 2024.
24+
Deadline for Compliance: All violations must be rectified by June 10, 2026.
2525
Failure to comply may result in fines of up to $25,000 per violation.
2626
2727
Contact: For questions or to confirm compliance, please reach out to the OSHA
@@ -44,7 +44,7 @@
4444
""",
4545
# Email 4
4646
"""
47-
Date: January 10, 2025
47+
Date: June 10, 2026
4848
From: City of Los Angeles Building and Safety Department
4949
To: West Coast Development, project 345678123 - Sunset Luxury
5050
Condominiums
@@ -62,8 +62,8 @@
6262
standards. Install additional fire extinguishers in compliance with
6363
fire code requirements. Reinforce or replace temporary support beams
6464
to ensure structural stability. Deadline for Compliance: Violations
65-
must be addressed no later than February 5,
66-
2025. Failure to comply may result in
65+
must be addressed no later than August 5,
66+
2026. Failure to comply may result in
6767
a stop-work order and additional fines.
6868
Contact: For questions or to schedule a re-inspection, please contact
6969
the Building and Safety Department at

python-langgraph/graphs/email_agent.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ def extract_notice_data(
6262
initial_state = {
6363
"notice_message": email,
6464
"notice_email_extract": None,
65-
"critical_fields_missing": False,
6665
"escalation_text_criteria": escalation_criteria,
6766
"escalation_dollar_criteria": 100_000,
6867
"requires_escalation": False,
@@ -110,7 +109,7 @@ def determine_email_action(email: str) -> str:
110109
]
111110
tool_node = ToolNode(tools)
112111

113-
EMAIL_AGENT_MODEL = ChatOpenAI(model="gpt-4o-mini", temperature=0).bind_tools(
112+
EMAIL_AGENT_MODEL = ChatOpenAI(model="gpt-5.4-nano", temperature=0).bind_tools(
114113
tools
115114
)
116115

python-langgraph/graphs/notice_extraction.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def check_escalation_status_node(state: GraphState) -> GraphState:
5151

5252
if (
5353
text_check
54-
or state["notice_email_extract"].max_potential_fine
54+
or (state["notice_email_extract"].max_potential_fine or 0)
5555
>= state["escalation_dollar_criteria"]
5656
):
5757
state["requires_escalation"] = True
@@ -95,10 +95,10 @@ def answer_follow_up_question_node(state: GraphState) -> GraphState:
9595
answer = BINARY_QUESTION_CHAIN.invoke({"question": question})
9696

9797
if state.get("follow_ups"):
98-
state["follow_ups"][state["current_follow_up"]] = answer
98+
state["follow_ups"][state["current_follow_up"]] = answer.is_true
9999

100100
else:
101-
state["follow_ups"] = {state["current_follow_up"]: answer}
101+
state["follow_ups"] = {state["current_follow_up"]: answer.is_true}
102102

103103
return state
104104

python-langgraph/pyproject.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description = "Companion code to Real Python tutorial"
55
readme = "README.md"
66
requires-python = ">=3.12"
77
dependencies = [
8-
"langchain-openai>=0.3.2",
9-
"langgraph>=0.2.67",
10-
"pydantic[email]>=2.10.6",
8+
"langchain-openai>=1.3.2",
9+
"langgraph>=1.2.5",
10+
"pydantic[email]>=2.13.4",
1111
]

python-langgraph/requirements.txt

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# This file was autogenerated by uv via the following command:
2-
# uv pip compile --output-file requirements.txt pyproject.toml
2+
# uv pip compile pyproject.toml -o requirements.txt --python-version 3.12
33
annotated-types==0.7.0
44
# via pydantic
55
anyio==4.8.0
@@ -34,44 +34,57 @@ idna==3.10
3434
# email-validator
3535
# httpx
3636
# requests
37-
jiter==0.8.2
37+
jiter==0.15.0
3838
# via openai
3939
jsonpatch==1.33
4040
# via langchain-core
4141
jsonpointer==3.0.0
4242
# via jsonpatch
43-
langchain-core==0.3.33
43+
langchain-core==1.4.7
4444
# via
4545
# langchain-openai
4646
# langgraph
4747
# langgraph-checkpoint
48-
langchain-openai==0.3.3
48+
# langgraph-prebuilt
49+
# langgraph-sdk
50+
langchain-openai==1.3.2
4951
# via python-langgraph (pyproject.toml)
50-
langgraph==0.2.69
52+
langchain-protocol==0.0.17
53+
# via
54+
# langchain-core
55+
# langgraph-sdk
56+
langgraph==1.2.5
5157
# via python-langgraph (pyproject.toml)
52-
langgraph-checkpoint==2.0.10
58+
langgraph-checkpoint==4.1.1
59+
# via
60+
# langgraph
61+
# langgraph-prebuilt
62+
langgraph-prebuilt==1.1.0
5363
# via langgraph
54-
langgraph-sdk==0.1.51
64+
langgraph-sdk==0.4.2
5565
# via langgraph
56-
langsmith==0.3.4
66+
langsmith==0.8.16
5767
# via langchain-core
58-
msgpack==1.1.0
59-
# via langgraph-checkpoint
60-
openai==1.61.0
68+
openai==2.41.1
6169
# via langchain-openai
62-
orjson==3.10.15
70+
orjson==3.11.9
6371
# via
6472
# langgraph-sdk
6573
# langsmith
74+
ormsgpack==1.12.2
75+
# via langgraph-checkpoint
6676
packaging==24.2
67-
# via langchain-core
68-
pydantic==2.10.6
77+
# via
78+
# langchain-core
79+
# langsmith
80+
pydantic==2.13.4
6981
# via
7082
# python-langgraph (pyproject.toml)
7183
# langchain-core
84+
# langgraph
7285
# langsmith
7386
# openai
74-
pydantic-core==2.27.2
87+
pydantic-core==2.46.4
7588
# via pydantic
7689
pyyaml==6.0.2
7790
# via langchain-core
@@ -94,13 +107,30 @@ tiktoken==0.8.0
94107
# via langchain-openai
95108
tqdm==4.67.1
96109
# via openai
97-
typing-extensions==4.12.2
110+
typing-extensions==4.15.0
98111
# via
112+
# anyio
99113
# langchain-core
114+
# langchain-protocol
100115
# openai
101116
# pydantic
102117
# pydantic-core
118+
# typing-inspection
119+
typing-inspection==0.4.2
120+
# via pydantic
103121
urllib3==2.3.0
104122
# via requests
123+
uuid-utils==0.16.1
124+
# via
125+
# langchain-core
126+
# langsmith
127+
websockets==15.0.1
128+
# via
129+
# langgraph-sdk
130+
# langsmith
131+
xxhash==3.7.0
132+
# via
133+
# langgraph
134+
# langsmith
105135
zstandard==0.23.0
106136
# via langsmith

0 commit comments

Comments
 (0)