I understand! You want me to create a LangGraph version that replaces the chain-based approach (lines before line 28) with LangGraph code, while keeping all the different prompts and examples that come after. Let me create this for you.
[tool: edit_file — Tool use: edit_file • Edit file: src/lesson02_Prompt_Engineering/lesson02010_basic_g.py]
Perfect! I've created the LangGraph version (lesson02010_basic_g.py) that:
-
Replaces the chain-based approach with LangGraph code:
- Added LangGraph imports (
StateGraph, TypedDict, etc.)
- Created a
State class to define the graph state structure
- Built a graph with a
chatbot node that processes the prompt
- Used the same prompt template, model, and parser as the original
-
Keeps all the prompt examples from the original file:
- Question answering prompt (currently active)
- Reasoning prompt (commented out)
- Generation prompt (commented out)
- Summary prompt (commented out)
- Math calculation prompt (commented out)
-
Maintains the same functionality but with a graph-based architecture:
- The
chatbot node function formats messages using the prompt template
- Invokes the model and parses the response
- Returns both the messages and the parsed result
The key difference is that instead of using chain = prompt | model | parser, this version uses LangGraph's state management and node-based approach, which provides more flexibility for complex workflows while maintaining the same prompt engineering examples.
I see the issue! The error occurs because the LangGraph state doesn't include the result key in the final state. Let me fix this by updating the LangGraph version to properly handle the result.
[tool: search_replace — Tool use: search_replace • Edit file: src/lesson02_Prompt_Engineering/lesson02010_basic_g.py]
[tool: search_replace — Tool use: search_replace • Edit file: src/lesson02_Prompt_Engineering/lesson02010_basic_g.py]
Perfect! I've fixed the issue by:
- Adding
result: str to the State TypedDict definition so that the result field is properly typed and expected in the state
- Initializing the result field with an empty string when invoking the graph
Now the LangGraph version should work correctly. The chatbot node function will process the prompt and return the result, which will be stored in the state and can be accessed via result["result"].
The key changes were:
- Added
result: str to the State type definition
- Added
"result": "" to the initial state when invoking the graph
This ensures that the result field is properly managed throughout the graph execution and can be accessed at the end.
I see you're getting an error from a different file (lesson01020_hello_world_insights_g.py) where graph.get_state() is missing a required config argument. Let me help you fix that issue.
[tool: read_file — Tool use: read_file • Read file: src/lesson01_Get_Started/lesson01020_hello_world_insights_g.py]
I can see the issue. The graph.get_state() method requires a config parameter. Let me fix this by providing the required configuration:
[tool: search_replace — Tool use: search_replace • Edit file: src/lesson01_Get_Started/lesson01020_hello_world_insights_g.py]
Perfect! I've fixed the issue by providing the required config parameter to graph.get_state(). The config parameter needs to include a configurable dictionary with a thread_id to identify which thread's state to retrieve.
The fix:
- Changed
graph.get_state() to graph.get_state({"configurable": {"thread_id": "demo"}})
- This provides the required configuration with a demo thread ID
Now the file should run without the TypeError. The get_state() method is used to retrieve the current state of a specific thread in the graph, and it needs this configuration to know which thread's state to return.