Skip to content

fix(core): handle subgraph nesting better in graph_mermaid #7907

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jasonphillips
Copy link

Currently, graph_mermaid can throw an error when subgraphs are deeply nested, due to its way of tracking subgraph names and its order of processing edges. We encountered this in production with a large graph which had a couple of levels of subgraphs and many edges.

See the nodes/edges fixture added in graph_mermaid.test.ts, which is a minimal reproduction of one part of our graph, and will throw the following error on main branch:

Found duplicate subgraph 'fooGenericSearch' -- this likely means that you're reusing a subgraph node with the same name. Please adjust your graph to have subgraph nodes with unique names

The graph (and original langgraph code that generated it) is valid, and doesn't have a duplicate subgraph, but the subgraph/prefix handling misrecognizes it as duplicate when traversing and tracking the set of subgraphs.

This PR fixes that, and can now render the graph correctly; and also pre-orders the edges so that subgraphs will appear in their correct nesting, which wasn't necessarily the case before.

Without the additional edge reordering, the test graph here was not properly nested:
image

due to incorrect ordering of subgraphs (excerpt):

      subgraph fooTool
      subgraph fooGenericSearch
         ...
      end
      subgraph fooSearchSource
         ...
      end
        ...
      end

With the fix, it is, thanks to ordering edges by depth before traversal:
image

          subgraph fooTool
          subgraph fooSearchSource
          subgraph fooGenericSearch
              ...
          end
              ...
          end
              ...
          end

I was able to test this rendering on our very large application graph and there were no unintended side effects / regressions; would be happy to have even more test cases for example graphs if needed--this particular script had little in the way of existing tests.

Copy link

vercel bot commented Mar 26, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
langchainjs-docs ✅ Ready (Inspect) Visit Preview Mar 31, 2025 8:56pm
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
langchainjs-api-refs ⬜️ Ignored (Inspect) Mar 31, 2025 8:56pm

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. auto:bug Related to a bug, vulnerability, unexpected error with an existing feature labels Mar 26, 2025
@jacoblee93
Copy link
Collaborator

Nice catch, thank you!

@jacoblee93 jacoblee93 changed the title fix: handle subgraph nesting better in graph_mermaid fix(core): handle subgraph nesting better in graph_mermaid Mar 30, 2025
Copy link
Collaborator

@jacoblee93 jacoblee93 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this makes sense - CC @nfcampos

"Please adjust your graph to have subgraph nodes with unique names."
);

if (seenSubgraphs.has(prefix)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably still throw in this case?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point; I restored this, and also needed to add one more protection to the recursion to prevent reaching deep paths twice

mermaidGraph += `\tsubgraph ${subgraph}\n`;
}

// all nested prefixes for this level, sorted by depth
const nestedPrefixes = sortPrefixesByDepth(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will need to take a closer look, but shouldn't all prefixes passed into sortPrefixesByDepth have the same length?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants