feat(litellm): add team name and user name support to LiteLLM credential#6487
feat(litellm): add team name and user name support to LiteLLM credential#6487raghava3107 wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces optional 'Team Name' and 'User Name' fields to the LiteLLM API credentials. These fields are then extracted and passed as custom headers ('x-litellm-team' and 'x-litellm-user') in the configuration of the ChatLiteLLM model. No review comments were provided, and there is no feedback to address.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
LiteLLM proxy supports team-based routing for billing and access control. This adds optional Team Name and User Name fields to the LiteLLM credential, and passes them as x-litellm-team and x-litellm-user headers in API requests. This enables organizations using LiteLLM proxy with team-scoped API keys to explicitly identify the team and user making requests, which is useful for: - Team-based cost tracking and billing - Per-team rate limiting - Audit logging Both fields are optional and backward-compatible — existing LiteLLM configurations continue to work without changes.
Replace team/user-specific fields with a generic Custom Headers field that supports any LiteLLM proxy header (x-litellm-tags, x-litellm-customer-id, x-litellm-end-user-id, x-litellm-spend-logs-metadata, etc.). Headers are passed as a JSON string in the credential and sent as defaultHeaders in the OpenAI-compatible client configuration. Malformed JSON is handled gracefully. Add comprehensive test suite covering: - Basic initialization with API key and model - Custom headers passed correctly to the client - Malformed JSON headers handled gracefully - Backward compatibility without headers - Optional parameters (maxTokens, topP, timeout)
Ensure JSON.parse result is a non-array object before using as headers. Rejects arrays, strings, and numbers that would parse but are invalid headers.
b07f0bd to
04484bc
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces support for custom headers in the LiteLLM chat model integration. It adds a new litellmCustomHeaders field to the LiteLLM credentials, parses this field in the model initialization, and forwards the headers to the underlying ChatOpenAI client. Comprehensive unit tests have also been added to cover various scenarios, including malformed JSON handling. The reviewer recommended making the custom headers parsing more robust by checking if the input is already an object before attempting to parse it as a string, preventing potential silent failures.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| let defaultHeaders: ICommonObject = {} | ||
| if (customHeadersRaw) { | ||
| try { | ||
| const parsed = JSON.parse(customHeadersRaw) | ||
| if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) { | ||
| defaultHeaders = parsed | ||
| } | ||
| } catch { | ||
| // ignore malformed JSON | ||
| } | ||
| } |
There was a problem hiding this comment.
If customHeadersRaw is already parsed or passed as an object (for example, when overridden via API/workflows or if the credential field is populated as an object), calling JSON.parse on it will throw an error and cause the headers to be silently ignored. To make this robust, we should check if customHeadersRaw is already an object before attempting to parse it as a string.
let defaultHeaders: ICommonObject = {}
if (customHeadersRaw) {
if (typeof customHeadersRaw === 'object' && !Array.isArray(customHeadersRaw)) {
defaultHeaders = customHeadersRaw
} else if (typeof customHeadersRaw === 'string') {
try {
const parsed = JSON.parse(customHeadersRaw)
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
defaultHeaders = parsed
}
} catch {
// ignore malformed JSON
}
}
}
Summary
Adds a Custom Headers field to the LiteLLM credential, enabling users to pass any LiteLLM proxy headers in API requests. Also adds a comprehensive test suite for the ChatLiteLLM node.
Why is this needed?
LiteLLM is an AI gateway/proxy that routes requests to 100+ LLM providers (OpenAI, Anthropic, Bedrock, etc.) through a unified API. Organizations use LiteLLM for:
LiteLLM supports several request headers for these features (docs):
x-litellm-tagsx-litellm-customer-idx-litellm-end-user-idx-litellm-spend-logs-metadataCurrently, Flowise's LiteLLM integration only supports an API key — there's no way to pass these headers to the proxy.
Solution
Credential (
LitellmApi.credential.ts):Custom Headersfield (JSON string){"x-litellm-tags": "team:activation,env:prod"}Chat model (
ChatLitellm.ts):defaultHeadersin the OpenAI-compatible client configurationTests (
ChatLitellm.test.ts) — 6 tests:Backward Compatibility
The Custom Headers field is optional. Existing LiteLLM configurations continue to work without any changes.
Files Changed
packages/components/credentials/LitellmApi.credential.tslitellmCustomHeadersfieldpackages/components/nodes/chatmodels/ChatLitellm/ChatLitellm.tspackages/components/nodes/chatmodels/ChatLitellm/ChatLitellm.test.tsTest Results