langchain/libs/core/langchain_core/runnables
Ant White e3ea365725
core: use friendlier names for duplicated nodes in mermaid output (#27747)
Thank you for contributing to LangChain!

- [x] **PR title**: "core: use friendlier names for duplicated nodes in
mermaid output"

- **Description:** When generating the Mermaid visualization of a chain,
if the chain had multiple nodes of the same type, the reid function
would replace their names with the UUID node_id. This made the generated
graph difficult to understand. This change deduplicates the nodes in a
chain by appending an index to their names.
- **Issue:** None
- **Discussion:**
https://github.com/langchain-ai/langchain/discussions/27714
- **Dependencies:** None

- [ ] **Add tests and docs**:  
- Currently this functionality is not covered by unit tests, happy to
add tests if you'd like


- [x] **Lint and test**: Run `make format`, `make lint` and `make test`
from the root of the package(s) you've modified. See contribution
guidelines for more: https://python.langchain.com/docs/contributing/

Additional guidelines:
- Make sure optional dependencies are imported within a function.
- Please do not add dependencies to pyproject.toml files (even optional
ones) unless they are required for unit tests.
- Most PRs should not touch more than one package.
- Changes should be backwards compatible.
- If you are adding something to community, do not re-import it in
langchain.

If no one reviews your PR within a few days, please @-mention one of
baskaryan, efriis, eyurtsev, ccurme, vbarda, hwchase17.

# Example Code:
```python
from langchain_core.runnables import RunnablePassthrough

def fake_llm(prompt: str) -> str: # Fake LLM for the example
    return "completion"

runnable = {
    'llm1':  fake_llm,
    'llm2':  fake_llm,
} | RunnablePassthrough.assign(
    total_chars=lambda inputs: len(inputs['llm1'] + inputs['llm2'])
)

print(runnable.get_graph().draw_mermaid(with_styles=False))
```

# Before
```mermaid
graph TD;
	Parallel_llm1_llm2_Input --> 0b01139db5ed4587ad37964e3a40c0ec;
	0b01139db5ed4587ad37964e3a40c0ec --> Parallel_llm1_llm2_Output;
	Parallel_llm1_llm2_Input --> a98d4b56bd294156a651230b9293347f;
	a98d4b56bd294156a651230b9293347f --> Parallel_llm1_llm2_Output;
	Parallel_total_chars_Input --> Lambda;
	Lambda --> Parallel_total_chars_Output;
	Parallel_total_chars_Input --> Passthrough;
	Passthrough --> Parallel_total_chars_Output;
	Parallel_llm1_llm2_Output --> Parallel_total_chars_Input;
```

# After
```mermaid
graph TD;
	Parallel_llm1_llm2_Input --> fake_llm_1;
	fake_llm_1 --> Parallel_llm1_llm2_Output;
	Parallel_llm1_llm2_Input --> fake_llm_2;
	fake_llm_2 --> Parallel_llm1_llm2_Output;
	Parallel_total_chars_Input --> Lambda;
	Lambda --> Parallel_total_chars_Output;
	Parallel_total_chars_Input --> Passthrough;
	Passthrough --> Parallel_total_chars_Output;
	Parallel_llm1_llm2_Output --> Parallel_total_chars_Input;
```
2024-10-31 16:52:00 -04:00
..
__init__.py core[minor]: Add BaseModel.rate_limiter, RateLimiter abstraction and in-memory implementation (#24669) 2024-07-26 03:03:34 +00:00
base.py core: Add ruff rules for error messages (EM) (#26965) 2024-10-07 22:12:28 +00:00
branch.py core: Add ruff rules for error messages (EM) (#26965) 2024-10-07 22:12:28 +00:00
config.py core: Add ruff rules for error messages (EM) (#26965) 2024-10-07 22:12:28 +00:00
configurable.py core: Add ruff rules for error messages (EM) (#26965) 2024-10-07 22:12:28 +00:00
fallbacks.py core: Add ruff rules for error messages (EM) (#26965) 2024-10-07 22:12:28 +00:00
graph_ascii.py core: Add ruff rules for error messages (EM) (#26965) 2024-10-07 22:12:28 +00:00
graph_mermaid.py core: add flake8-bandit (S) ruff rules to core (#27368) 2024-10-24 22:33:41 +00:00
graph_png.py core: Add ruff rules for error messages (EM) (#26965) 2024-10-07 22:12:28 +00:00
graph.py core: use friendlier names for duplicated nodes in mermaid output (#27747) 2024-10-31 16:52:00 -04:00
history.py core: Add ruff rules for error messages (EM) (#26965) 2024-10-07 22:12:28 +00:00
learnable.py
passthrough.py core[patch]: fix init of RunnableAssign (#26903) 2024-10-01 14:21:54 -04:00
retry.py core: Add ruff rules for pycodestyle Warning (W) (#26964) 2024-09-30 09:31:43 -04:00
router.py core: Add ruff rules for error messages (EM) (#26965) 2024-10-07 22:12:28 +00:00
schema.py core: Add ruff rules for pycodestyle Warning (W) (#26964) 2024-09-30 09:31:43 -04:00
utils.py core: Add ruff rules for error messages (EM) (#26965) 2024-10-07 22:12:28 +00:00