Implementation:24mlight A Share investment Agent AgentState TypedDict
| Knowledge Sources | |
|---|---|
| Domains | AI_Agents, Workflow_Orchestration |
| Last Updated | 2026-10-10 14:00 GMT |
Overview
Concrete data structure defining the shared state contract for all agents in the LangGraph workflow, with annotated reducers for message accumulation and dictionary merging.
Description
The AgentState TypedDict defines the schema of the shared state that flows through the LangGraph StateGraph. It has three fields, each with a reducer that defines how updates from parallel agents are merged:
- messages: Uses
operator.addto concatenate lists ofBaseMessageobjects. When multiple parallel agents return messages, they are accumulated. - data: Uses
merge_dictsto shallow-merge dictionaries. Later values overwrite earlier ones. - metadata: Uses the same
merge_dictsreducer.
The merge_dicts function is a simple shallow merge: {**a, **b}.
This is a Pattern Doc — it documents the shared state interface that all agents must adhere to.
Usage
Use this pattern when:
- Defining the state schema for a LangGraph workflow
- Ensuring consistent state structure across all agents
- Understanding how parallel agent outputs are merged
Code Reference
Source Location
- Repository: A_Share_investment_Agent
- File: src/agents/state.py
- Lines: L12-21
Signature
from typing import Annotated, Any, Dict, Sequence, TypedDict
import operator
from langchain_core.messages import BaseMessage
def merge_dicts(a: Dict[str, Any], b: Dict[str, Any]) -> Dict[str, Any]:
return {**a, **b}
class AgentState(TypedDict):
messages: Annotated[Sequence[BaseMessage], operator.add]
data: Annotated[Dict[str, Any], merge_dicts]
metadata: Annotated[Dict[str, Any], merge_dicts]
Import
from src.agents.state import AgentState
I/O Contract
State Fields
| Field | Type | Reducer | Description |
|---|---|---|---|
| messages | Sequence[BaseMessage] | operator.add (concatenation) | Accumulated agent messages (HumanMessage objects) |
| data | Dict[str, Any] | merge_dicts (shallow merge) | Shared data: ticker, prices, financial_metrics, portfolio, etc. |
| metadata | Dict[str, Any] | merge_dicts (shallow merge) | Run metadata: show_reasoning, run_id, trace_dir, etc. |
Common data Fields
| Key | Type | Description |
|---|---|---|
| ticker | str | Stock code (6-digit) |
| portfolio | dict | {"cash": float, "stock": int} |
| start_date | str | Analysis start date (YYYY-MM-DD) |
| end_date | str | Analysis end date (YYYY-MM-DD) |
| prices | list[dict] | OHLCV price records |
| financial_metrics | dict | Financial indicators |
| financial_line_items | dict | Financial statements |
| num_of_news | int | Number of news articles |
Usage Examples
Defining a Graph with AgentState
from langgraph.graph import StateGraph
from src.agents.state import AgentState
workflow = StateGraph(AgentState)
workflow.add_node("my_agent", my_agent_func)
workflow.set_entry_point("my_agent")
workflow.add_edge("my_agent", END)
app = workflow.compile()
result = app.invoke({
"messages": [],
"data": {"ticker": "002848", "portfolio": {"cash": 100000, "stock": 0}},
"metadata": {"show_reasoning": True}
})
Agent Return Pattern
def my_agent(state: AgentState) -> dict:
# Read state
ticker = state["data"]["ticker"]
prices = state["data"]["prices"]
# Process...
# Return updates (merged via reducers)
return {
"messages": [HumanMessage(content="result", name="my_agent")],
"data": {"my_analysis": result_dict},
"metadata": state["metadata"],
}