onRequest on triggered when using custom client in useStream #349

Closed
opened 2026-02-15 18:16:06 -05:00 by yindo · 0 comments
Owner

Originally created by @SeveHo on GitHub (Aug 28, 2025).

It seems like useStream does not trigger onRequest when calling stream.submit() and using a custom client that has onRequest implemented.

Here is my code:

this._client = new Client({
      apiKey: ...,
      apiUrl: ...,
      onRequest: async (_, init) => {
        // Always get fresh token for each request
        // Always add organization ID header
        init.headers = {
          ...init.headers,
          Authorization: `Bearer ${this.getToken()}`
        };
        return init;
      },
    });
const streamValue = useStream({
      client: agentStore.client,
      assistantId,
      threadId: currentThreadId,
      initialValues,
      messagesKey: "messages",
      onThreadId: async (id) => {
        agentStore.navigateToThread(id);
      },
      onError: handleError,
    });

    return (
      <StreamContext.Provider value={streamValue}>
        {children}
      </StreamContext.Provider>
    );
 stream.submit({ messages }, {
      metadata: {...},
      context: {...},
      config: {
        recursion_limit: 50,
      },
      checkpoint: parentCheckpoint,
      streamMode: ["messages"],
      optimisticValues: (prev) => ({
        ...prev,
        messages: [
          ...(parentValues?.messages || prev.messages || []),
          ...(messages || []),
        ],
      }),
    });
  };

But when checking the request on submit my Authorization header actually never gets added. If i use the client directly like so in a different instance onRequest works perfectly fine:

const threads = await this._client.threads.search({
        metadata: {...},
        limit: this._threadsLimit,
        offset: offset,
        sortBy: "updated_at",
        sortOrder: "desc",
      });

I was able to work around this by using my own fetch implementation for now but thought it's still worth flagging. Here is the workaround:

this._client = new Client({
    apiKey: ...,
    apiUrl: ...,
    callerOptions: {
      fetch: async (url: string | URL, init?: RequestInit) => {
        // Always get fresh token for each request
        const headers = {
          ...init?.headers,
          Authorization: `Bearer ${this.getToken()}`,
        }

        // Use the global fetch with our custom headers
        return fetch(url, {
          ...init,
          headers,
        });
      },
    },
});

Error Message and Stack Trace (if applicable)

No response

System Info

"@langchain/core": "^0.3.72",
"@langchain/langgraph": "^0.4.7",
"@langchain/langgraph-sdk": "^0.0.112",
Originally created by @SeveHo on GitHub (Aug 28, 2025). It seems like useStream does not trigger onRequest when calling stream.submit() and using a custom client that has onRequest implemented. Here is my code: ```ts this._client = new Client({ apiKey: ..., apiUrl: ..., onRequest: async (_, init) => { // Always get fresh token for each request // Always add organization ID header init.headers = { ...init.headers, Authorization: `Bearer ${this.getToken()}` }; return init; }, }); ``` ```ts const streamValue = useStream({ client: agentStore.client, assistantId, threadId: currentThreadId, initialValues, messagesKey: "messages", onThreadId: async (id) => { agentStore.navigateToThread(id); }, onError: handleError, }); return ( <StreamContext.Provider value={streamValue}> {children} </StreamContext.Provider> ); ``` ```ts stream.submit({ messages }, { metadata: {...}, context: {...}, config: { recursion_limit: 50, }, checkpoint: parentCheckpoint, streamMode: ["messages"], optimisticValues: (prev) => ({ ...prev, messages: [ ...(parentValues?.messages || prev.messages || []), ...(messages || []), ], }), }); }; ``` But when checking the request on submit my Authorization header actually never gets added. If i use the client directly like so in a different instance onRequest works perfectly fine: ```ts const threads = await this._client.threads.search({ metadata: {...}, limit: this._threadsLimit, offset: offset, sortBy: "updated_at", sortOrder: "desc", }); ``` I was able to work around this by using my own fetch implementation for now but thought it's still worth flagging. Here is the workaround: ```ts this._client = new Client({ apiKey: ..., apiUrl: ..., callerOptions: { fetch: async (url: string | URL, init?: RequestInit) => { // Always get fresh token for each request const headers = { ...init?.headers, Authorization: `Bearer ${this.getToken()}`, } // Use the global fetch with our custom headers return fetch(url, { ...init, headers, }); }, }, }); ``` ### Error Message and Stack Trace (if applicable) _No response_ ### System Info "@langchain/core": "^0.3.72", "@langchain/langgraph": "^0.4.7", "@langchain/langgraph-sdk": "^0.0.112",
yindo closed this issue 2026-02-15 18:16:06 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraphjs#349