Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/node-trace-policy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@langchain/langgraph": patch
---

feat(langgraph): add per-node `tracePolicy` input/output processors and `omitPayload`

Transform the payloads recorded on a node's own trace run while retaining its span and timing. Processors receive raw values and fall back to the original payload if they throw. Graph state, root runs, and child runs remain unchanged when processors do not mutate their arguments.

Matches Python's callback-level behavior: transforms also affect chain events and message streaming, so omitting outputs can suppress messages returned directly by nodes and omitting inputs can affect message deduplication.
6 changes: 6 additions & 0 deletions libs/langgraph-core/src/graph/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ import type {
CachePolicy,
RetryPolicy,
TimeoutPolicy,
TracePolicy,
} from "../pregel/utils/index.js";
import { coerceTimeoutPolicy } from "../pregel/utils/index.js";
import { isPregelLike } from "../pregel/utils/subgraph.js";
Expand Down Expand Up @@ -201,6 +202,7 @@ export type StateGraphNodeSpec<RunInput, RunOutput> = NodeSpec<
> &
NodePolicies & {
input?: StateDefinition;
tracePolicy?: TracePolicy;
};

/**
Expand All @@ -222,6 +224,8 @@ export type StateGraphAddNodeOptions<
Update = unknown,
> = {
input?: InputSchema;
/** Transform this node's trace payloads. See {@link TracePolicy} for scope and streaming behavior. */
tracePolicy?: TracePolicy;
/**
* Optional node-level error handler. Runs only after this node's
* {@link RetryPolicy} is exhausted. Receives a {@link NodeError} with the
Expand Down Expand Up @@ -1221,6 +1225,7 @@ export class StateGraph<
retryPolicy: options?.retryPolicy,
cachePolicy,
timeout: coerceTimeoutPolicy(options?.timeout),
tracePolicy: options?.tracePolicy,
metadata: options?.metadata,
input: inputSpec ?? this._schemaDefinition,
subgraphs: isPregelLike(runnable)
Expand Down Expand Up @@ -1957,6 +1962,7 @@ export class CompiledStateGraph<
retryPolicy: node?.retryPolicy,
cachePolicy,
timeout: node?.timeout,
tracePolicy: node?.tracePolicy,
subgraphs: node?.subgraphs,
ends: node?.ends,
isErrorHandler: node?.isErrorHandler,
Expand Down
68 changes: 67 additions & 1 deletion libs/langgraph-core/src/pregel/read.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { describe, expect, it, vi } from "vitest";
import { RunnableConfig } from "@langchain/core/runnables";
import { CONFIG_KEY_READ } from "../constants.js";
import { CONFIG_KEY_READ, CONFIG_KEY_SEND } from "../constants.js";
import { LastValue } from "../channels/last_value.js";
import { ChannelRead, PregelNode } from "./read.js";
import { ChannelWrite } from "./write.js";
import { RunnableCallable } from "../utils.js";
import { FakeTracer } from "../tests/utils.js";
import { omitPayload } from "./utils/index.js";

describe("ChannelRead", () => {
it("should read a single channel value", async () => {
Expand Down Expand Up @@ -111,6 +114,69 @@ describe("ChannelRead", () => {
});

describe("PregelNode", () => {
it("preserves trace policies across join and every pipe branch", async () => {
const tracePolicy = {
processInputs: omitPayload,
processOutputs: omitPayload,
};
const base = new PregelNode({
channels: { input: "input" },
triggers: ["input"],
tracePolicy,
});
const action = new RunnableCallable({
func: (input: number) => input + 1,
trace: false,
});
const bound = base.pipe(action);
const twice = bound.pipe(action);
const writer = new ChannelWrite([{ channel: "output", value: "written" }]);
const withWriter = twice.pipe(writer);
const joined = withWriter.join(["extra"]);
for (const node of [base, bound, twice, withWriter, joined]) {
expect(node.tracePolicy).toBe(tracePolicy);
}
const tracer = new FakeTracer();
const send = vi.fn();
expect(
await joined
.getNode()
?.invoke(1, {
callbacks: [tracer],
configurable: { [CONFIG_KEY_SEND]: send },
})
).toBe(3);
expect(send).toHaveBeenCalledWith([["output", "written"]]);
expect(tracer.runs[0].inputs).toEqual({});
expect(tracer.runs[0].outputs).toEqual({});
expect(tracer.runs[0].child_runs[0].inputs).toEqual({ input: 1 });
});

it("applies policies to multiple writers and preserves single-step shortcuts", async () => {
const tracePolicy = {
processInputs: omitPayload,
processOutputs: omitPayload,
};
const base = { channels: ["input"], triggers: ["input"], tracePolicy };
const action = new RunnableCallable({
func: (input: number) => input + 1,
trace: false,
});
expect(new PregelNode(base).getNode()).toBeUndefined();
expect(new PregelNode({ ...base, writers: [action] }).getNode()).toBe(
action
);
expect(new PregelNode({ ...base, bound: action }).getNode()).toBe(action);
const tracer = new FakeTracer();
const node = new PregelNode({
...base,
writers: [action, action],
}).getNode();
expect(await node?.invoke(1, { callbacks: [tracer] })).toBe(3);
expect(tracer.runs[0].inputs).toEqual({});
expect(tracer.runs[0].outputs).toEqual({});
});

it("should create a node that subscribes to channels", () => {
const node = new PregelNode({
channels: ["input", "context"],
Expand Down
29 changes: 26 additions & 3 deletions libs/langgraph-core/src/pregel/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ import {
import { CONFIG_KEY_READ } from "../constants.js";
import { ChannelWrite } from "./write.js";
import { RunnableCallable } from "../utils.js";
import type { CachePolicy, RetryPolicy, TimeoutPolicy } from "./utils/index.js";
import type {
CachePolicy,
RetryPolicy,
TimeoutPolicy,
TracePolicy,
} from "./utils/index.js";
import { RunnableSeq } from "./runnable.js";

export class ChannelRead<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -87,6 +93,7 @@ interface PregelNodeArgs<RunInput, RunOutput> extends Partial<
retryPolicy?: RetryPolicy;
cachePolicy?: CachePolicy;
timeout?: TimeoutPolicy;
tracePolicy?: TracePolicy;
subgraphs?: Runnable[];
ends?: string[];
/** Whether this node is an auto-generated node-level error handler. */
Expand Down Expand Up @@ -131,6 +138,8 @@ export class PregelNode<

timeout?: TimeoutPolicy;

tracePolicy?: TracePolicy;

subgraphs?: Runnable[];

ends?: string[];
Expand All @@ -151,6 +160,7 @@ export class PregelNode<
retryPolicy,
cachePolicy,
timeout,
tracePolicy,
tags,
subgraphs,
ends,
Expand Down Expand Up @@ -184,6 +194,7 @@ export class PregelNode<
this.retryPolicy = retryPolicy;
this.cachePolicy = cachePolicy;
this.timeout = timeout;
this.tracePolicy = tracePolicy;
this.subgraphs = subgraphs;
this.ends = ends;
this.isErrorHandler = isErrorHandler;
Expand Down Expand Up @@ -214,19 +225,27 @@ export class PregelNode<

getNode(): Runnable<RunInput, RunOutput> | undefined {
const writers = this.getWriters();
const sequence = (
fields: ConstructorParameters<
typeof RunnableSequence<RunInput, RunOutput>
>[0]
) =>
this.tracePolicy
? new RunnableSeq({ ...fields, tracePolicy: this.tracePolicy })
: new RunnableSequence(fields);
if (this.bound === defaultRunnableBound && writers.length === 0) {
return undefined;
} else if (this.bound === defaultRunnableBound && writers.length === 1) {
return writers[0];
} else if (this.bound === defaultRunnableBound) {
return new RunnableSequence({
return sequence({
first: writers[0],
middle: writers.slice(1, writers.length - 1),
last: writers[writers.length - 1],
omitSequenceTags: true,
});
} else if (writers.length > 0) {
return new RunnableSequence({
return sequence({
first: this.bound,
middle: writers.slice(0, writers.length - 1),
last: writers[writers.length - 1],
Expand Down Expand Up @@ -259,6 +278,7 @@ export class PregelNode<
retryPolicy: this.retryPolicy,
cachePolicy: this.cachePolicy,
timeout: this.timeout,
tracePolicy: this.tracePolicy,
});
}

Expand All @@ -280,6 +300,7 @@ export class PregelNode<
retryPolicy: this.retryPolicy,
cachePolicy: this.cachePolicy,
timeout: this.timeout,
tracePolicy: this.tracePolicy,
});
} else if (this.bound === defaultRunnableBound) {
return new PregelNode<RunInput, Exclude<NewRunOutput, Error>>({
Expand All @@ -293,6 +314,7 @@ export class PregelNode<
retryPolicy: this.retryPolicy,
cachePolicy: this.cachePolicy,
timeout: this.timeout,
tracePolicy: this.tracePolicy,
});
} else {
return new PregelNode<RunInput, Exclude<NewRunOutput, Error>>({
Expand All @@ -306,6 +328,7 @@ export class PregelNode<
retryPolicy: this.retryPolicy,
cachePolicy: this.cachePolicy,
timeout: this.timeout,
tracePolicy: this.tracePolicy,
});
}
}
Expand Down
Loading
Loading