Ariel Nurieli
Ariel Nurieli

Reputation: 11

AgentState not properly initialized in LangGraph graph/node

I’m working on a LangGraph-based agent and running into an issue with state management when using LangGraph Studio. Specifically, I’m getting a KeyError: 'RAG_attempts' when my agent hits a downstream node, even though the key ('RAG_attempts') is initialized in my AgentState module. After some debugging, it seems like the key isn't even being passed into the first node, which might be causing this issue downstream.

  1. Error When Running LangGraph Studio:

When I run the agent in LangGraph Studio, I get this error when it hits the evaluator node:

def evaluator(state: AgentState, *, config: RunnableConfig) -> dict:
    print("⚙️ Current Process: evaluator")
    configuration = AgentConfiguration.from_runnable_config(config)

    reval_attempts = state["RAG_attempts"]  # KeyError happens here

Error Message:

KeyError: 'RAG_attempts'

AgentState class for reference:

from dataclasses import dataclass, field
from typing import TypedDict, Annotated, List, Literal
from langchain_core.documents import Document
from langchain_core.messages import AnyMessage
from langgraph.graph import add_messages

@dataclass(kw_only=True)
class AgentState(TypedDict):
    """Represents the input state for the agent."""
    
    messages: Annotated[List[AnyMessage], add_messages]

    label: dict = field(default_factory=lambda: {"type": "general", "logic": ""})
   
    document_evaluation: str = field(default="GOOD")
    
    documents: List[Document] = field(default_factory=list)
    
    search_results: str = field(default="")

    RAG_attempts: int = field(default=0)

So Far:

  1. Added Debug Prints in all nodes to check if key exists: I added print statements in earlier nodes to check where the issue starts. It turns out that the RAG_attempts key doesn’t even get passed into the first node in the sequence, so it’s not surprising that it fails in the evaluator node later on.
  2. Trying to Access Other Attributes: When I check for the existence of other state attributes (label, documents, RAG_attempts, etc.), it seems that none are being passed through as When I try to access them (like state["label"] or state["documents"]), I get similar KeyError exceptions, which suggests that the entire AgentState isn’t being passed correctly.

Now, I looked at many examples on github, youtube, and langgraph documentation, and it looks like what I have is pretty standard. I create a state with the keys I want to follow through the graph, and declare it as such:

self.workflow = StateGraph(AgentState)

Would love some help figuring this out!

Upvotes: 1

Views: 60

Answers (0)

Related Questions