Skip to content
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

langgraph-api doesn't support dict format for graph specification #4178

Open
gfortaine opened this issue Apr 5, 2025 · 0 comments
Open

langgraph-api doesn't support dict format for graph specification #4178

gfortaine opened this issue Apr 5, 2025 · 0 comments
Assignees

Comments

@gfortaine
Copy link

gfortaine commented Apr 5, 2025

Hi team,

PR #4164 updated the LangGraph CLI to allow specifying graphs in langgraph.json using a dictionary format:

{
  "dependencies": ["./my_agent"],
  "graphs": {
    "agent": {
      "path": "./my_agent/agent.py:graph",
      "description": "this is my agent description"
    }
  },
  "env": ".env"
}

However, the langgraph-api package, specifically the collect_graphs_from_env function in langgraph_api/graph.py, was not updated to handle this new format. It still assumes the value for each graph is a string (path:variable).

This leads to the following error when langgraph-api tries to parse a langgraph.json using the dictionary format:

Traceback (most recent call last):
  File ".../langgraph_api/lifespan.py", line 49, in lifespan
    await collect_graphs_from_env(True)
  File ".../langgraph_api/graph.py", line 198, in collect_graphs_from_env
    path_or_module, variable = value.rsplit(":", maxsplit=1)
                               ^^^^^^^^^^^^
AttributeError: 'dict' object has no attribute 'rsplit'

The collect_graphs_from_env function needs to be updated to check the type of the value and extract the path string if it's a dictionary.

Here's a suggested implementation for the relevant part of the function:

async def collect_graphs_from_env(register: bool = False) -> None:
    # ... (previous code) ...

    if paths_str:
        specs = []
        for key, value in json.loads(paths_str).items():
            # Check if value is a dictionary (new format) or string (old format)
            if isinstance(value, dict):
                path_string = value.get("path")
                if not path_string or not isinstance(path_string, str):
                    raise ValueError(
                        f"Invalid dictionary format for graph '{key}'. Missing or invalid 'path' key."
                    )
            elif isinstance(value, str):
                path_string = value
            else:
                raise ValueError(
                    f"Invalid format for graph '{key}'. Expected string or dictionary, got {type(value)}."
                )

            try:
                path_or_module, variable = path_string.rsplit(":", maxsplit=1)
            except ValueError as e:
                raise ValueError(
                    f"Invalid path string '{path_string}' for graph '{key}'."
                    " Did you miss a variable name?\n"
                    " Expected format: 'path/to/file.py:variable_name' or 'my.module:variable_name'"
                ) from e

            # Determine if it's a module path or file path based on the presence of '/'
            is_module_path = "/" not in path_or_module
            specs.append(
                GraphSpec(
                    key,
                    module=path_or_module if is_module_path else None,
                    path=None if is_module_path else path_or_module,
                    variable=variable,
                    config=config_per_graph.get(key),
                )
            )
    # ... (rest of the function) ...

Could you please update langgraph-api to align with the CLI changes? Thanks!

@gfortaine gfortaine changed the title langgraph-api doesn't support dict format for graph specification from PR #4164 langgraph-api doesn't support dict format for graph specification Apr 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants