Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to define a conditional edge #413

Closed
4 tasks done
jindalAnuj opened this issue May 7, 2024 · 4 comments
Closed
4 tasks done

Unable to define a conditional edge #413

jindalAnuj opened this issue May 7, 2024 · 4 comments

Comments

@jindalAnuj
Copy link

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.

Example Code

    class WorkFlow():
	def __init__(self):
		nodes = Nodes()
		workflow = StateGraph(PersonState)
		workflow.add_node("userInput", nodes.userInput)
		workflow.add_node("text2PersonInfo", nodes.text2PersonInfo)
		workflow.add_node("personInfoValidate", nodes.personInfoValidate)
		workflow.add_node("missingDataAgent", nodes.missingDataAgent)
		workflow.set_entry_point("userInput")
		workflow.add_edge('userInput', 'text2PersonInfo')
		workflow.add_edge('text2PersonInfo', 'personInfoValidate')
		workflow.add_conditional_edges(
			"personInfoValidate",
			nodes.personInfoValidate,
			{
				"incomplete": "missingDataAgent",
				"end": END
			}
		)
		self.app = workflow.compile()


 Traceback (most recent call last):
  File "/opt/homebrew/Cellar/python@3.10/3.10.14/Frameworks/Python.framework/Versions/3.10/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/opt/homebrew/Cellar/python@3.10/3.10.14/Frameworks/Python.framework/Versions/3.10/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/Users/jindal/AppDevelopment/30DaysofAI/LLMHandsOn/lang_graph/main.py", line 15, in <module>
    app.invoke({})
  File "/Users/jindal/Library/Caches/pypoetry/virtualenvs/llm-handson-m_vF4p_v-py3.10/lib/python3.10/site-packages/langgraph/pregel/__init__.py", line 1171, in invoke
    for chunk in self.stream(
  File "/Users/jindal/Library/Caches/pypoetry/virtualenvs/llm-handson-m_vF4p_v-py3.10/lib/python3.10/site-packages/langgraph/pregel/__init__.py", line 774, in stream
    _panic_or_proceed(done, inflight, step)
  File "/Users/jindal/Library/Caches/pypoetry/virtualenvs/llm-handson-m_vF4p_v-py3.10/lib/python3.10/site-packages/langgraph/pregel/__init__.py", line 1260, in _panic_or_proceed
    raise exc
  File "/opt/homebrew/Cellar/python@3.10/3.10.14/Frameworks/Python.framework/Versions/3.10/lib/python3.10/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/Users/jindal/Library/Caches/pypoetry/virtualenvs/llm-handson-m_vF4p_v-py3.10/lib/python3.10/site-packages/langchain_core/runnables/base.py", line 2499, in invoke
    input = step.invoke(
  File "/Users/jindal/Library/Caches/pypoetry/virtualenvs/llm-handson-m_vF4p_v-py3.10/lib/python3.10/site-packages/langgraph/utils.py", line 88, in invoke
    ret = context.run(self.func, input, **kwargs)
  File "/Users/jindal/Library/Caches/pypoetry/virtualenvs/llm-handson-m_vF4p_v-py3.10/lib/python3.10/site-packages/langgraph/graph/graph.py", line 74, in _route
    destinations = [self.ends[r] for r in result]
  File "/Users/jindal/Library/Caches/pypoetry/virtualenvs/llm-handson-m_vF4p_v-py3.10/lib/python3.10/site-packages/langgraph/graph/graph.py", line 74, in <listcomp>
    destinations = [self.ends[r] for r in result]
TypeError: unhashable type: 'dict'
[tool.poetry.dependencies]
python = ">=3.10.0,<3.11"
ollama = "^0.1.8"
load-dotenv = "^0.1.0"
langgraph = "0.0.45"
langchain = "^0.1.17"

Error Message and Stack Trace (if applicable)

No response

Description

whenever i remove conditional node it works fine

System Info

System Information

OS: Darwin
OS Version: Darwin Kernel Version 22.2.0: Fri Nov 11 02:04:44 PST 2022; root:xnu-8792.61.2~4/RELEASE_ARM64_T8103
Python Version: 3.10.14 (main, Mar 19 2024, 21:46:16) [Clang 15.0.0 (clang-1500.1.0.2.5)]

Package Information

langchain_core: 0.1.52
langchain: 0.1.17
langchain_community: 0.0.37
langsmith: 0.1.54
langchain_openai: 0.0.5
langchain_text_splitters: 0.0.1
langgraph: 0.0.45

Packages not installed (Not Necessarily a Problem)

The following packages were not found:

langserve

@hinthornw
Copy link
Contributor

What does personInfoValidate look like?

@jindalAnuj
Copy link
Author

jindalAnuj commented May 7, 2024

        ask_for = []
        status = "end"
        print('personInfoValidate',state)
        # Check if fields are empty 
        # for field, value in state["person"].dict().items():
        #     if value in [None, "", 0]:  # You can add other 'empty' conditions as per your requirements
        #         print(f"Field '{field}' is empty.")
        #         ask_for.append(f'{field}')
        # if ask_for:
        #     status = "incomplete"
        return {
            **state,
            "status": status,
            # "ask_for": ask_for
        }

some commented code for testing

@dylanlap98
Copy link

I'm having this same problem.

Pip versions:
...
langgraph 0.0.46
langchain-core 0.1.52
...

Code:


from langgraph.graph import StateGraph, END
from typing import TypedDict, Annotated
 
class GraphState(TypedDict):
    num_steps: int
    email: str
    classification: str
    technology: str
 
def graph_printer(state):
    print("-- GRAPH PRINTER --")
    print(f"classification: {state['classification']}")
    print(f"technology: {state['technology']}")
    print(f"num_steps: {state['num_steps']}")
    print()
    return
 
def router_1(state):    #: List[BaseMessage]
    #technology = state['technology']
 
    ## LLM call would go here (for now simulate response):
    technology = "Sagemaker"
 
    print(technology)
    if 'aws' in technology.lower():
        print("RUNNING AWS\n")
        return "aws"
        #return {"technology": "aws"} # trying other formats to fix
    else:
        print("RUNNING OTHER\n")
        return "other"
        #return {"technology": "other"}
        #return {"input": "other"}
 
def lane_switch(state):
    print(" --- RAN LANE SWITCH --- ")
    print()
    return
   
testgraph = StateGraph(GraphState)
 
testgraph.add_node("state_printer_1", graph_printer)
testgraph.add_node("router", router_1)
testgraph.add_node("state_printer_2", graph_printer)
testgraph.add_node("lane_switcher", lane_switch) # only run if aws
testgraph.set_entry_point("state_printer_1")
testgraph.add_edge("state_printer_1", "router")
testgraph.add_conditional_edges(
    "router",
    router_1,
    {
        "aws": "lane_switcher",
        "other": "state_printer_2"
    }
)
testgraph.add_edge("lane_switcher", "state_printer_2")
testgraph.add_edge("state_printer_2", END)
 

testapp = testgraph.compile()
 
email  = "HELLO THERE AWS USER! Thanks." # simulate email
inputs = {"email": email, "num_steps": 0}
testapp.invoke(inputs)

Output error:

langgraph.errors.InvalidUpdateError: Expected dict, got other

Hoping this example helps..
-Dylan LaPierre

@DevTimBo
Copy link

You can fix this by adding a next_step to your graph instead of just passing the route function to the conditional edge
image

image

I hope this helps

@langchain-ai langchain-ai locked and limited conversation to collaborators May 15, 2024
@hinthornw hinthornw converted this issue into discussion #460 May 15, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants