Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
662053d
Add LangGraph Hello World sample
mfateev Dec 27, 2025
18b995a
Add README for react_agent sample
mfateev Dec 27, 2025
f8f51a0
LangGraph: Use ClientConfig for client initialization in all samples
mfateev Dec 27, 2025
6b96e09
LangGraph: Add react_agent and approval_workflow sample files
mfateev Dec 27, 2025
2e5c82e
LangGraph: Add README for approval_workflow and update react_agent RE…
mfateev Dec 27, 2025
0432967
LangGraph: Add notification activity and CLI response tool to approva…
mfateev Dec 27, 2025
ca793bf
LangGraph: Wrap model with temporal_model for durable LLM calls in re…
mfateev Dec 27, 2025
5adef7d
LangGraph: Update react_agent sample to use create_durable_react_agent
mfateev Dec 27, 2025
3b62683
LangGraph: Update react_agent to use new activity_options API
mfateev Dec 27, 2025
1fb4682
LangGraph: Use native create_react_agent instead of wrappers
mfateev Dec 27, 2025
c18f9cb
LangGraph: Update SAMPLES_PROPOSAL.md to reflect simplified API
mfateev Dec 27, 2025
d3fe116
LangGraph: Use create_agent from langchain.agents (create_react_agent…
mfateev Dec 27, 2025
771b46a
LangGraph: Update react_agent sample to demonstrate multi-step agenti…
mfateev Dec 28, 2025
f579c0d
LangGraph: Add RAG sample and update documentation
mfateev Dec 28, 2025
57110cf
LangGraph: Update README with branch installation instructions
mfateev Dec 28, 2025
0302320
LangGraph: Remove unnecessary hasattr check in agentic_rag
mfateev Dec 28, 2025
e82d0a5
LangGraph samples: Use create_agent instead of deprecated create_reac…
mfateev Dec 29, 2025
3475e6f
langgraph-supervisor dependency added
mfateev Dec 29, 2025
7db14fe
LangGraph: Add deep research, plan-and-execute, and reflection samples
mfateev Dec 29, 2025
0beeb3c
LangGraph samples: Standardize usage instructions in READMEs
mfateev Dec 29, 2025
3e6df8f
LangGraph: Deep research sample uses real DuckDuckGo web search
mfateev Dec 29, 2025
d772f0a
LangGraph samples: Flatten directory structure to single level
mfateev Dec 29, 2025
fe7134d
LangGraph samples: Update README run commands for flattened structure
mfateev Dec 29, 2025
58516c1
Fix lint errors in langgraph samples
mfateev Dec 30, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Temporal Python Samples

## Client Initialization Pattern

Use the `ClientConfig` pattern for client initialization to support environment-based configuration:

```python
from temporalio.client import Client
from temporalio.envconfig import ClientConfig

config = ClientConfig.load_client_connect_config()
config.setdefault("target_host", "localhost:7233")
client = await Client.connect(**config)
```

This pattern allows configuration via environment variables while providing sensible defaults.

## LangGraph Guidelines

### Agent Creation

- **DO NOT** use `create_react_agent` from `langgraph.prebuilt` - it is deprecated
- **USE** `create_agent` from `langchain.agents` instead

```python
# Wrong (deprecated)
from langgraph.prebuilt import create_react_agent
agent = create_react_agent(model=model, tools=[...], prompt="...")

# Correct
from langchain.agents import create_agent
agent = create_agent(model=model, tools=[...], system_prompt="...")
```
63 changes: 63 additions & 0 deletions langgraph_samples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Temporal LangGraph Samples

These samples demonstrate the Temporal LangGraph integration - combining LangGraph's agent framework with Temporal's durable execution.

> **Note:** The LangGraph integration is currently available as a preview feature in the `langgraph_plugin` branch of the SDK repository.

## Overview

The integration combines:
- **Temporal workflows** for orchestrating agent control flow and state management
- **LangGraph** for defining agent graphs with conditional logic, cycles, and state

This approach ensures that AI agent workflows are durable, observable, and can handle failures gracefully.

## Prerequisites

- Temporal server [running locally](https://docs.temporal.io/cli/server#start-dev)
- Python 3.9+
- [uv](https://docs.astral.sh/uv/) package manager (recommended)

## Installation

Since the LangGraph integration is currently in a branch, you need to install from the branch repositories.

### Running the Samples

1. Clone this samples repository:
```bash
git clone -b langgraph_plugin https://github.com/mfateev/samples-python.git
cd samples-python
```

2. Install dependencies:
```bash
uv sync --group langgraph
```

3. Install the SDK from the `langgraph-plugin` branch:
```bash
uv pip install "temporalio @ git+https://github.com/mfateev/sdk-python.git@langgraph-plugin"
```

4. Start a local Temporal server:
```bash
temporal server start-dev
```

5. Navigate to a sample directory and follow its README for specific instructions

## Examples

Each directory contains a complete example with its own README for detailed instructions:

| Sample | Description |
|--------|-------------|
| [hello_world](./hello_world/) | Simple starter example demonstrating basic plugin setup and graph registration |
| [react_agent](./react_agent/) | ReAct agent pattern with tool calling and multi-step reasoning |
| [approval_workflow](./approval_workflow/) | Human-in-the-loop with interrupt/resume for approval workflows |
| [supervisor](./supervisor/) | Multi-agent supervisor pattern coordinating specialized agents |
| [agentic_rag](./agentic_rag/) | Retrieval-augmented generation with document grading and query rewriting |
| [deep_research](./deep_research/) | Multi-step research with web search and iterative refinement |
| [plan_and_execute](./plan_and_execute/) | Plan-and-execute pattern with structured step execution |
| [reflection](./reflection/) | Self-reflection pattern for iterative improvement |
1 change: 1 addition & 0 deletions langgraph_samples/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Temporal LangGraph Samples
111 changes: 111 additions & 0 deletions langgraph_samples/agentic_rag/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# Agentic RAG

An intelligent RAG (Retrieval Augmented Generation) agent that decides when to retrieve documents, grades their relevance, and rewrites queries when needed.

## What This Sample Demonstrates

- **`create_agent` pattern**: Uses LangChain's `create_agent` for the retrieval step
- **Document grading**: Retrieved documents are evaluated for relevance before generating answers
- **Query rewriting**: If documents aren't relevant, the query is reformulated and retrieval is retried
- **Durable execution**: Graph nodes run as Temporal activities with automatic retries
- **Crash recovery**: If the worker fails, execution resumes from the last completed node

## How It Works

The graph implements an agentic RAG pattern with `create_agent` for retrieval:

```
+-------+
| START |
+---+---+
|
v
+-----------------+
+--------->| retrieve_agent |<--------+
| | (create_agent) | |
| +-----------------+ |
| | |
| "not relevant" "relevant" |
| | | |
+---+---+ | | +----+
|rewrite|<-----+ +---->| END|
+-------+ +----+
^
|
+-----+----+
| generate |
+----------+
```

1. **retrieve_agent**: Uses `create_agent` to decide whether to retrieve and fetch documents (runs as single activity)
2. **grade_documents**: Conditional edge that evaluates document relevance
3. **generate**: Produces the final answer using relevant documents (runs as activity)
4. **rewrite**: Reformulates the query if documents weren't relevant, then retries (runs as activity)

### Subgraph Behavior

The Temporal LangGraph plugin automatically detects subgraphs (like `create_agent`) and executes their **inner nodes as separate activities**. This means:

- The retrieve_agent subgraph's `model` and `tools` nodes run as separate Temporal activities
- Each node has its own retry/timeout configuration
- If the worker crashes during retrieval, execution resumes from the last completed inner node
- You get full durability without manually adding separate nodes

The sample includes a knowledge base with documents about:
- LangGraph features and capabilities
- Temporal concepts (workflows, activities, signals, queries)
- ReAct pattern for AI agents
- Agentic RAG patterns
- Human-in-the-loop workflows

## Prerequisites

- Temporal server running locally (`temporal server start-dev`)
- OpenAI API key set: `export OPENAI_API_KEY=your-key`

## Running the Example

First, start the worker:
```bash
uv run langgraph_samples/agentic_rag/run_worker.py
```

Then, in a separate terminal, run the workflow:
```bash
uv run langgraph_samples/agentic_rag/run_workflow.py
```

## Expected Output

```
The ReAct (Reasoning and Acting) pattern is an approach where an LLM alternates
between thinking about what to do and taking actions. The loop consists of:
1. Think - The LLM reasons about the current state and what action to take
2. Act - Execute the chosen action (e.g., call a tool)
3. Observe - Process the result of the action
4. Repeat until the task is complete

When combined with Temporal, the ReAct pattern gains durability because each
graph node runs as a Temporal activity. This means progress is saved after each
node completes, and if the worker crashes, execution resumes from the last
completed node.
```

## Sample Queries

You can modify the query in `run_workflow.py` to test different scenarios:

- **Retrieval needed**: "What is LangGraph and how does it work?"
- **Multi-topic**: "How do Temporal signals work with human-in-the-loop workflows?"
- **Off-topic (no retrieval)**: "What's 2 + 2?" (agent responds directly)
- **Needs rewriting**: "Tell me about durable stuff" (vague query gets rewritten)

## Architecture Notes

The sample uses an in-memory vector store with sample documents for simplicity. In production, you would:

1. Use a persistent vector store (Pinecone, Weaviate, Chroma, etc.)
2. Load documents from your actual knowledge base
3. Configure appropriate chunk sizes and embedding models

The document grading step adds latency but significantly improves answer quality by ensuring the RAG system only uses relevant context.
1 change: 1 addition & 0 deletions langgraph_samples/agentic_rag/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Agentic RAG sample with document grading and query rewriting."""
Loading
Loading