Skip to content

Conversation

@taiichiro-nagase
Copy link

@taiichiro-nagase taiichiro-nagase commented Jan 22, 2026

Problem

Self-hosted developers need to customize HTTP request behavior for testing, proxying, logging, or using custom fetch implementations (e.g., in specific environments or with custom headers/interceptors). Currently, there's no way to inject a custom fetch function into the Langfuse client.

Changes

Added support for custom fetch functions throughout the Langfuse SDK:

  1. New fetch option in LangfuseClientParams: Users can now pass a custom fetch function when initializing LangfuseClient
  2. Type-safe implementation: Added fetch?: typeof fetch to all Options interfaces across the codebase without using any casts
  3. Propagation through the stack: The custom fetch function is properly passed from LangfuseClient → LangfuseAPIClient → all 26 resource clients → core.fetcher

Example usage:

const customFetch = async (input, init) => {
  console.log('Making request to:', input);
  return fetch(input, init);
};

const langfuse = new LangfuseClient({
  publicKey: "pk_...",
  secretKey: "sk_...",
  fetch: customFetch,
});

Release info Sub-libraries affected

Bump level

  • Major
  • Minor
  • Patch

Libraries affected

  • All of them
  • langfuse
  • langfuse-node

Changelog notes

  • Added support for custom fetch functions via the fetch option in LangfuseClientParams, enabling request customization for testing, proxying, and logging use cases

Disclaimer: Experimental PR review

Greptile Summary

This PR adds support for custom fetch functions throughout the Langfuse SDK, enabling users to inject custom HTTP request behavior for testing, proxying, logging, or using alternative fetch implementations.

Key changes:

  • Added fetch?: typeof fetch option to LangfuseClientParams interface with clear documentation and example usage
  • Properly propagated the custom fetch function through the entire call chain: LangfuseClientLangfuseAPIClient → all 26 resource clients → core.fetcher
  • Implemented type-safe fallback logic in Fetcher.ts: uses custom fetch if provided, otherwise falls back to getFetchFn()
  • Maintained consistency across all resource clients with identical implementation pattern

Implementation quality:

  • Type safety is maintained throughout with typeof fetch (no any casts)
  • All 26 resource clients were updated uniformly
  • The feature is additive and backward compatible (optional parameter)
  • Documentation includes practical example showing the use case

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The implementation is clean, type-safe, and follows a consistent pattern across all 29 files. The change is purely additive (optional parameter), maintains backward compatibility, and uses proper TypeScript typing with typeof fetch. All 26 resource clients were updated uniformly with the same pattern, showing thorough and systematic implementation. The fallback logic is simple and correct.
  • No files require special attention

Important Files Changed

Filename Overview
packages/client/src/LangfuseClient.ts Added fetch option to LangfuseClientParams interface with proper documentation and passed it to LangfuseAPIClient constructor
packages/core/src/api/Client.ts Added fetch option to LangfuseAPIClient.Options interface with documentation, properly typed as typeof fetch
packages/core/src/api/core/fetcher/Fetcher.ts Added fetch option to Fetcher.Args interface and implemented fallback logic to use custom fetch or default getFetchFn()
packages/core/src/api/api/resources/health/client/Client.ts Added fetch option to Health.Options interface and propagated this._options.fetch to core.fetcher calls
packages/core/src/api/api/resources/prompts/client/Client.ts Added fetch option to Prompts.Options interface and propagated to all 4 method calls (list, get, create, update)
packages/core/src/api/api/resources/ingestion/client/Client.ts Added fetch option to Ingestion.Options interface and propagated to batch method's core.fetcher call

Sequence Diagram

sequenceDiagram
    participant User
    participant LangfuseClient
    participant LangfuseAPIClient
    participant ResourceClient
    participant Fetcher
    participant CustomFetch

    User->>LangfuseClient: new LangfuseClient({fetch: customFetch})
    LangfuseClient->>LangfuseAPIClient: new LangfuseAPIClient({fetch: customFetch})
    LangfuseAPIClient->>LangfuseAPIClient: Store fetch in _options
    
    User->>LangfuseClient: api.prompts.list()
    LangfuseClient->>ResourceClient: prompts.list()
    ResourceClient->>Fetcher: core.fetcher({fetch: this._options.fetch})
    
    alt Custom fetch provided
        Fetcher->>CustomFetch: Use args.fetch
        CustomFetch-->>Fetcher: Response
    else No custom fetch
        Fetcher->>Fetcher: Use getFetchFn() (default)
        Fetcher-->>Fetcher: Response
    end
    
    Fetcher-->>ResourceClient: API Response
    ResourceClient-->>LangfuseClient: Data
    LangfuseClient-->>User: Result
Loading

@vercel
Copy link

vercel bot commented Jan 22, 2026

@taiichiro-nagase is attempting to deploy a commit to the langfuse Team on Vercel.

A member of the Team first needs to authorize it.

@CLAassistant
Copy link

CLAassistant commented Jan 22, 2026

CLA assistant check
All committers have signed the CLA.

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Jan 22, 2026

Greptile found no issues!

From now on, if a review finishes and we haven't found any issues, we will not post anything, but you can confirm that we reviewed your changes in the status check section.

This feature can be toggled off in your Code Review Settings by deselecting "Create a status check for each PR".

@taiichiro-nagase taiichiro-nagase force-pushed the feature/support-fetch-option branch from 676f530 to dc634d2 Compare January 25, 2026 23:53
@hassiebp hassiebp self-requested a review January 26, 2026 10:53
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