Skip to content

fix(langchain-agents): build graph node models from the node's provider - #321

Open
ld-paul wants to merge 3 commits into
mainfrom
paul/langchain-graph-default-models
Open

ld-paul wants to merge 3 commits into
mainfrom
paul/langchain-graph-default-models

Conversation

@ld-paul

@ld-paul ld-paul commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Human

When a LangChain agent graph ran through to_lang_graph() without a model_factory, every node got ChatOpenAI, whatever provider the node's config named, so an Anthropic or Bedrock node either failed or quietly ran on OpenAI. Now each node gets the same model a single-agent call on that config would get: ChatAnthropic for Anthropic, ChatBedrockConverse for Bedrock, and ChatOpenAI otherwise, and one graph can mix providers. A model_factory you pass still wins.

Bot

What changed

  • packages/langchain-agents/src/launchdarkly_ai_langchain_agents/native_graph.py: the inline ChatOpenAI construction in _traverse_node is replaced by a call to the handler's own _make_default_chat_model(node.config). No second factory exists. A node therefore gets the handler's per-provider class, its forwarded-parameter list and renames (for example max_tokens_to_sample to max_tokens, stop to stop_sequences), the Bedrock model.region inference-profile prefix, and the pip install "launchdarkly-ai-langchain-agents[anthropic]" or [aws] hint when the extra is missing. The now-unused imports of the OpenAI key lists, select_forwarded_parameters, and model_parameters are gone. The model_factory docstring no longer says the default is ChatOpenAI.
  • This mirrors the Go SDK, whose graph runner routes through the same langchainwire.DefaultModel its handlers use.
  • Because the graph calls the shared factory rather than its own copy, the Gemini branch that feat(langchain): route Gemini configs to ChatGoogleGenerativeAI #320 adds to _make_default_chat_model reaches graph nodes through this change once both merge, with no further edit.
  • Docs: packages/langchain-agents/README.md and packages/langchain-agents/agents.md now say graph nodes pick their model by provider. Both also dropped a stale claim that hand-off tools are bound with parallel_tool_calls=False; the code never passes it, because Bedrock Converse rejects it, and routes only the first hand-off instead. The README model_factory example now reads node.config, since GraphNode is an object, not a dict.

Graph behavior checked against non-OpenAI models

  • Tool binding: bind_tools is called with no extra kwargs, and the hand-off tool's injected tool_call_id stays out of the schema the model sees. Covered with the real ChatAnthropic and ChatBedrockConverse classes.
  • Message formatting: a node entered by hand-off gets provider-neutral history (user, assistant, user), which ChatAnthropic._get_request_payload accepts with the node's system prompt lifted into system.
  • Usage: _extract_usage reads usage_metadata, which every LangChain chat model fills, so it is unchanged.
  • Spans: the native graph emits only the launchdarkly.graph span, which carries no provider or finish-reason attribute, so nothing there depended on OpenAI.
  • Structured output is not used on the graph path.

Tests

New classes in packages/langchain-agents/tests/test_native_graph.py, in the style of the existing handler tests (constructors mocked through sys.modules):

  • TestNativeGraphDefaultModelFollowsProvider: an Anthropic node builds ChatAnthropic with the renamed forwarded parameters; a Bedrock node builds ChatBedrockConverse with the region-prefixed model id; an OpenAI, empty, or missing provider keeps ChatOpenAI; a three-node graph gives each node its own provider's class; a model_factory wins for every provider; and a missing langchain-anthropic or langchain-aws surfaces the install-hint ImportError from invoke() and tracks $ld:ai:graph:invocation_failure.
  • TestNativeGraphRealProviderModels: a router hands off through a real StateGraph with the real ChatAnthropic and ChatBedrockConverse classes, with only _agenerate replaced.

Seven of the new tests fail on main and pass here.

Verification

All run from the repo root, matching .github/workflows/ci.yml:

  • uv run ruff check .: all checks passed
  • uv run ruff format --check .: 258 files already formatted
  • uv run mypy packages/*/src: no issues in 107 source files
  • uv run pytest: 4255 passed, 24 skipped
  • uv run python scripts/api_report.py --check: 14 API reports are current (no public signature changed)
  • make build: all packages built
  • make check-packaging: passed

Also merged this branch with #320 locally: no conflicts, the langchain-agents suite passes (272 tests), and a Gemini graph node builds ChatGoogleGenerativeAI.

Not verified: no node was run against a live Anthropic, Bedrock, or OpenAI endpoint.

🤖 Generated with Claude Code


Note

Overview
to_lang_graph() no longer forces ChatOpenAI on every node when model_factory is omitted. Each node now calls the handler’s _make_default_chat_model(node.config), so Anthropic, Bedrock, and OpenAI (or unknown) nodes get the same chat class, forwarded parameters, region-prefixed Bedrock ids, and optional-extra install hints as a single-agent invocation. A custom model_factory still overrides that default.

Docs are aligned with GraphNode attribute access (not dict keys), per-provider default models in mixed graphs, and hand-off routing (no parallel_tool_calls=False; only the first transfer in a turn routes). uv.lock bumps several package versions (e.g. launchdarkly-ai-langchain-agents 1.0.1).

Tests add broad coverage for provider selection, missing extras, real ChatAnthropic / ChatBedrockConverse hand-offs, and a three-provider mixed graph.

Reviewed by Cursor Bugbot for commit 5a7b6f4. Bugbot is set up for automated code reviews on this repo. Configure here.

to_lang_graph() built ChatOpenAI for every node that had no model_factory,
whatever the node config's provider.name said, so an Anthropic or Bedrock
node failed or ran on OpenAI. Each node's default model now comes from the
handler's _make_default_chat_model, the same factory a single-agent call
uses, so a node gets the same model class, forwarded parameters, Bedrock
region prefix, and missing-extra ImportError. A caller's model_factory
still wins.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@ld-paul
ld-paul requested a review from jeffdupont November 23, 2026 20:28

@jeffdupont jeffdupont left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, this is a real bug: a graph node with a non-OpenAI provider used to get ChatOpenAI anyway. The fix and the mixed-provider tests look right.

Should fix: update the shared cross-SDK contract with this. The contract still describes the ChatOpenAI default for the JS and Python toLangGraph when no model factory is given. The fix itself looks right (each node now gets the model its own provider calls for, as Go already does), so please update the contract with or right after this PR: per-node provider routing, the fallback for an unrecognised provider name, parameter forwarding, and mixed-provider graphs.

Should fix: the root README still has the broken example. You fixed node["config"] to node.config in packages/langchain-agents/README.md, but the root README.md (around line 373) still has lambda node: ChatOpenAI(model=node["config"]["model"]["name"]), which fails because GraphNode is an object. Please fix it there too and add the provider-routing note to that section.

)
kwargs["model"] = model_cfg.get("name") or "gpt-4o"
chat_model = lc_openai.ChatOpenAI(**kwargs)
chat_model = _make_default_chat_model(node.config)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is the behaviour change the shared contract still describes as ChatOpenAI (see the summary): fine as a fix; please update the contract with or right after it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed. I'm updating the shared contract in a separate ai-sdks-monorepo PR covering per-node provider routing, the fallback for an unrecognised provider, parameter forwarding, and mixed-provider graphs. I'll link it here when it's up.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The contract update is in https://github.com/launchdarkly/ai-sdks-monorepo/pull/170. It replaces the ChatOpenAI default with per-node routing through the same table as the handlers, and this PR follows it. 5a7b6f4 adds the graph-span ERROR and mixed-provider tests it asks for.

…outing

GraphDefinition and GraphNode are objects, so the traverse and
to_lang_graph examples read attributes instead of dict keys. The
to_lang_graph section now says each node's default model follows its
provider.name.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@ld-paul

ld-paul commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Fixed the root README in 288a044: the to_lang_graph example now uses node.config, and the section notes that each node's default model follows its provider.name (ChatAnthropic, ChatBedrockConverse, or ChatOpenAI for any other provider). The traverse example above it had the same dict-key bug on GraphDefinition and GraphNode, so I fixed that too. The contract update will follow in a separate PR.

… mixed-provider run

Match the per-node routing rules in ai-sdks-monorepo TESTING.md (PR 170):
a missing optional package now also asserts the import error is kept as
the cause and the launchdarkly.graph span ends with ERROR, and a
three-provider graph (Anthropic, Bedrock, OpenAI) runs a hand-off through
a real StateGraph with one model of each class built.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@ld-paul

ld-paul commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

The contract change is up for review: https://github.com/launchdarkly/ai-sdks-monorepo/pull/170. It replaces the ChatOpenAI graph default with per-node provider routing, covering the fallback for an unrecognised name, parameter forwarding, mixed-provider graphs, and the missing-package error. This PR already matches it, because the graph builds each node's model with the same _make_default_chat_model the agents handler uses. 5a7b6f4 adds the tests the contract asks for:

  • A missing langchain-anthropic or langchain-aws makes invoke() raise the install hint, keeps the import error as its cause, tracks $ld:ai:graph:invocation_failure, and ends the launchdarkly.graph span with ERROR.
  • A three-node graph naming Anthropic, Bedrock, and OpenAI builds one model of each class through a real StateGraph. The hand-off reaches the Bedrock node with the router's turns rewritten to plain text.

A Gemini node gets the Gemini route from #320 through the same function, with nothing extra needed here. I'll add a missing langchain-google-genai graph test once that lands.

🤖 Generated with Claude Code

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

Successfully merging this pull request may close these issues.

2 participants