Skip to content

feat: implement type inference for tool streaming#2004

Open
Colin Francis (colifran) wants to merge 3 commits intomainfrom
colifran/tool-stream-types
Open

feat: implement type inference for tool streaming#2004
Colin Francis (colifran) wants to merge 3 commits intomainfrom
colifran/tool-stream-types

Conversation

@colifran
Copy link
Contributor

@colifran Colin Francis (colifran) commented Feb 26, 2026

Summary

Adds type inference for toolProgress in useStream, so that useStream() produces typed tool names, inputs, streaming data, and results based on the agent's tool definitions without manual casting or explicit type parameters.

Problem

When using useStream(), the toolProgress array typed all fields as unknown and name as string. Users had no way to get typed narrowing on tool progress entries based on the agent's actual tool definitions. For streaming tools that use AsyncGenerator<YieldT, ReturnT>, the yield and return types were completely erased.

Solution

  • New ToolProgress generic type with discriminated union on state ("starting" | "running" | "completed" | "error"), enabling TypeScript narrowing while keeping all fields optional for backward compatibility.
  • InferToolMapFromAgent type utility in ui/types.ts that extracts { [toolName]: { input, data, result } } from an agent's tools array, including AsyncGenerator yield/return types for streaming tools.
  • DeriveToolProgress and GetToolProgressType helpers in types.stream.ts that convert an inferred tool map into a typed discriminated union of ToolProgress entries.
  • Added optional ToolMap to BagTemplate in types.template.ts, wired through InferBag in ui/stream/index.ts. Uses Omit to avoid index signature pollution from the base template.
  • Updated toolProgress property type in BaseStream and UseStream to use GetToolProgressType[].
  • Fixed handleToolEvent in react/stream.lgp.tsx to explicitly set undefined for fields that don't apply in each state transition, preventing stale values from leaking across states via object spread.

Testing

New type-level tests (stream.test-d.ts) verify tool progress inference:

  • Simple agent infers tool name and input; data/result remain unknown for non-streaming tools
  • Multi-tool agent infers all tool entries with correct input types
  • Streaming tool (AsyncGenerator) infers typed data and result
  • useStream toolProgress has literal tool names (not generic string)
  • State narrowing (state === "running") produces typed data and result

Example

This example shows how these changes enable type inference for tool names and tool events:
type-inference

@changeset-bot
Copy link

changeset-bot bot commented Feb 26, 2026

⚠️ No Changeset found

Latest commit: da250ae

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 26, 2026

Open in StackBlitz

@langchain/langgraph-checkpoint

npm i https://pkg.pr.new/langchain-ai/langgraphjs/@langchain/langgraph-checkpoint@2004

@langchain/langgraph-checkpoint-mongodb

npm i https://pkg.pr.new/langchain-ai/langgraphjs/@langchain/langgraph-checkpoint-mongodb@2004

@langchain/langgraph-checkpoint-postgres

npm i https://pkg.pr.new/langchain-ai/langgraphjs/@langchain/langgraph-checkpoint-postgres@2004

@langchain/langgraph-checkpoint-redis

npm i https://pkg.pr.new/langchain-ai/langgraphjs/@langchain/langgraph-checkpoint-redis@2004

@langchain/langgraph-checkpoint-sqlite

npm i https://pkg.pr.new/langchain-ai/langgraphjs/@langchain/langgraph-checkpoint-sqlite@2004

@langchain/langgraph-checkpoint-validation

npm i https://pkg.pr.new/langchain-ai/langgraphjs/@langchain/langgraph-checkpoint-validation@2004

create-langgraph

npm i https://pkg.pr.new/langchain-ai/langgraphjs/create-langgraph@2004

@langchain/langgraph-api

npm i https://pkg.pr.new/langchain-ai/langgraphjs/@langchain/langgraph-api@2004

@langchain/langgraph-cli

npm i https://pkg.pr.new/langchain-ai/langgraphjs/@langchain/langgraph-cli@2004

@langchain/langgraph

npm i https://pkg.pr.new/langchain-ai/langgraphjs/@langchain/langgraph@2004

@langchain/langgraph-cua

npm i https://pkg.pr.new/langchain-ai/langgraphjs/@langchain/langgraph-cua@2004

@langchain/langgraph-supervisor

npm i https://pkg.pr.new/langchain-ai/langgraphjs/@langchain/langgraph-supervisor@2004

@langchain/langgraph-swarm

npm i https://pkg.pr.new/langchain-ai/langgraphjs/@langchain/langgraph-swarm@2004

@langchain/langgraph-ui

npm i https://pkg.pr.new/langchain-ai/langgraphjs/@langchain/langgraph-ui@2004

@langchain/langgraph-sdk

npm i https://pkg.pr.new/langchain-ai/langgraphjs/@langchain/langgraph-sdk@2004

commit: da250ae

CustomEventType?: unknown;
UpdateType?: unknown;
MetaType?: unknown;
ToolMap?: Record<

Choose a reason for hiding this comment

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

nit: can we call this ToolTypes? also if we could update the jsdoc

Copy link
Member

Choose a reason for hiding this comment

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

Thank you 👍

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.

3 participants