Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LangServe JS client - forward Http-Only cookies when calling stream #712

Open
jccarles opened this issue Jul 17, 2024 · 1 comment
Open

Comments

@jccarles
Copy link

Hello,

We are using langserve js client the following way

const remoteChain = new RemoteRunnable({
    url: url,
    options: {
      headers:someHeader,
    },
  });

Then we stream the request as

remoteChain.stream(
        {
          chat_history: chat_history,
          question: question
        },
        {
          metadata: {
            run_id: id,
          },
        },
      );

Digging in the code it looks like stream is calling under the hood js fetch function the following way as seen here: https://github.com/langchain-ai/langchainjs/blob/dbb3b59623ee5ce4bf675bbf41a6e3e044ae0802/langchain-core/src/runnables/remote.ts#L278

private async post<Body>(path: string, body: Body) {
    return fetch(`${this.url}${path}`, {
      method: "POST",
      body: JSON.stringify(serialize(body)),
      headers: {
        "Content-Type": "application/json",
        ...this.options?.headers,
      },
      signal: AbortSignal.timeout(this.options?.timeout ?? 60000),
    });
  }

The best would be to be able to pass an additional parameter to fetch: credentials see here section how to pass cookies.
The updated version could be:

private async post<Body>(path: string, body: Body) {
    return fetch(`${this.url}${path}`, {
      method: "POST",
      body: JSON.stringify(serialize(body)),
      headers: {
        "Content-Type": "application/json",
        ...this.options?.headers,
      },
      signal: AbortSignal.timeout(this.options?.timeout ?? 60000),
      credentials: this.options?.credentials ?? 'same-origin',  // same-origin is fetch default credentials value
    });
  }

with and update to remoteRunnableOption as

type RemoteRunnableOptions = {
  timeout?: number;
  headers?: Record<string, unknown>;
  credentials?: string;
};

or hardcoded for example:

private async post<Body>(path: string, body: Body) {
    return fetch(`${this.url}${path}`, {
      method: "POST",
      body: JSON.stringify(serialize(body)),
      headers: {
        "Content-Type": "application/json",
        ...this.options?.headers,
      },
      signal: AbortSignal.timeout(this.options?.timeout ?? 60000),
      credentials: 'include',
    });
  }

Thank you !

@maxms93
Copy link

maxms93 commented Aug 20, 2024

You can configure this in the fetchRequestOptions

https://v02.api.js.langchain.com/classes/_langchain_core.runnables_remote.RemoteRunnable.html

const remoteChain = new RemoteRunnable({
   url: url,
    fetchRequestOptions: {credentials: 'include'}
});

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

No branches or pull requests

2 participants