"""
Pattern 15.04: Temporal Workflow
Time-aware orchestration with delays
"""
from typing import TypedDict
from langgraph.graph import StateGraph, END
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage
class TemporalWorkflowState(TypedDict):
"""State for Temporal Workflow pattern."""
input: str
output: str
metadata: dict
def agent_function(state: TemporalWorkflowState) -> TemporalWorkflowState:
"""Temporal Workflow agent implementation."""
llm = ChatOpenAI()
prompt = f"Process using Temporal Workflow: {state['input']}"
response = llm.invoke([HumanMessage(content=prompt)])
state["output"] = str(response.content)
state["metadata"] = {"pattern": "Temporal Workflow", "status": "completed"}
return state
def create_temporal_workflow():
"""Create Temporal Workflow workflow."""
workflow = StateGraph(TemporalWorkflowState)
workflow.add_node("agent", agent_function)
workflow.set_entry_point("agent")
workflow.add_edge("agent", END)
return workflow.compile()
if __name__ == "__main__":
workflow = create_temporal_workflow()
result = workflow.invoke({"input": "Test Temporal Workflow", "output": "", "metadata": {}})
print(f"\n=== Temporal Workflow Pattern ===\nOutput: {result['output'][:200]}...")
Agentic Orchestration Pattern: Temporal Workflow
04 Temporal Workflow Pattern – Detailed Documentation
Overview
File: 15_temporal/04_temporal_workflow.py
Pattern Category: 15 Temporal
Implementation: LangGraph/CrewAI
Pattern Description
Pattern 15.04: Temporal Workflow Time-aware orchestration with delays
Code Structure
Imports
from typing import TypedDict
from langgraph.graph import StateGraph, END
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage
Purpose of imports:
– TypedDict: For type-safe state definitions
– StateGraph: LangGraph’s graph-based workflow orchestration
– ChatOpenAI: OpenAI LLM integration
– HumanMessage: LangChain message formatting
State Definition
class TemporalWorkflowState(TypedDict):
"""State for Temporal Workflow pattern."""
input: str
output: str
metadata: dict
def agent_function(state: TemporalWorkflowState) -> TemporalWorkflowState:
TemporalWorkflowState: State for Temporal Workflow pattern.
State Fields:
– input: Stores input
– output: Stores output
– metadata: Stores metadata
– def agent_function(state: Stores def agent function(state
– prompt = f"Process using Temporal Workflow: Stores prompt = f”Process using Temporal Workflow
– state["metadata"] = {"pattern": Stores state[“metadata”] = {“pattern”
Functions
agent_function()
Line: 18
Arguments: state
Temporal Workflow agent implementation.
def agent_function(state: TemporalWorkflowState) -> TemporalWorkflowState:
"""Temporal Workflow agent implementation."""
llm = ChatOpenAI()
prompt = f"Process using Temporal Workflow: {state['input']}"
response = llm.invoke([HumanMessage(content=prompt)])
state["output"] = str(response.content)
state["metadata"] = {"pattern": "Temporal Workflow", "status": "completed"}
return state
def create_temporal_workflow():
create_temporal_workflow()
Line: 28
Arguments: “
Create Temporal Workflow workflow.
def create_temporal_workflow():
"""Create Temporal Workflow workflow."""
workflow = StateGraph(TemporalWorkflowState)
workflow.add_node("agent", agent_function)
workflow.set_entry_point("agent")
workflow.add_edge("agent", END)
return workflow.compile()
if __name__ == "__main__":
Line-by-Line Explanation
Line 5: Import statement
from typing import TypedDict
Imports TypedDict for creating type-safe state dictionaries.
Line 6: Import statement
from langgraph.graph import StateGraph, END
Imports StateGraph – the core LangGraph class for building workflows.
Line 7: Import statement
from langchain_openai import ChatOpenAI
Imports ChatOpenAI for LLM interactions.
Line 8: Import statement
from langchain_core.messages import HumanMessage
Imports HumanMessage for formatting prompts to the LLM.
Line 11: Class definition
class TemporalWorkflowState(TypedDict):
Defines the state structure using TypedDict for type safety.
Line 12: Module docstring
"""State for Temporal Workflow pattern."""
Describes the pattern and its purpose.
Line 18: Function agent_function definition
def agent_function(state: TemporalWorkflowState) -> TemporalWorkflowState:
Defines the agent_function function that processes the workflow state.
Line 19: Module docstring
"""Temporal Workflow agent implementation."""
Describes the pattern and its purpose.
Line 28: Function create_temporal_workflow definition
def create_temporal_workflow():
Defines the create_temporal_workflow function that processes the workflow state.
Line 29: Module docstring
"""Create Temporal Workflow workflow."""
Describes the pattern and its purpose.
Usage Example
if __name__ == "__main__":
workflow = create_temporal_workflow()
result = workflow.invoke({"input": "Test Temporal Workflow", "output": "", "metadata": {}})
print(f"\n=== Temporal Workflow Pattern ===\nOutput: {result['output'][:200]}...")
Key Concepts
- State Management: Uses TypedDict to maintain workflow state
- Graph-Based Flow: LangGraph orchestrates execution flow
- Agent Functions: Each node processes and updates state
- LLM Integration: Uses ChatOpenAI for intelligent processing
Pattern Benefits
- ✅ Type-safe state management
- ✅ Clear workflow visualization
- ✅ Modular and reusable components
- ✅ Easy to test and debug
- ✅ Scalable architecture
Related Patterns
- See other patterns in the same category
- Explore combination possibilities
- Check the main documentation for pattern relationships