-
-
Notifications
You must be signed in to change notification settings - Fork 24.5k
feat(litellm): add team name and user name support to LiteLLM credential #6487
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
Open
raghava3107
wants to merge
3
commits into
FlowiseAI:main
Choose a base branch
from
raghava3107:feature/litellm-team-user-headers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+274
−2
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
251 changes: 251 additions & 0 deletions
251
packages/components/nodes/chatmodels/ChatLitellm/ChatLitellm.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,251 @@ | ||
| jest.mock('@langchain/openai', () => ({ | ||
| ChatOpenAI: jest.fn().mockImplementation((fields) => ({ fields })) | ||
| })) | ||
|
|
||
| jest.mock('../../../src/utils', () => ({ | ||
| getBaseClasses: jest.fn().mockReturnValue(['BaseChatModel']), | ||
| getCredentialData: jest.fn(), | ||
| getCredentialParam: jest.fn() | ||
| })) | ||
|
|
||
| jest.mock('../ChatOpenAI/FlowiseChatOpenAI', () => ({ | ||
| ChatOpenAI: jest.fn().mockImplementation((_id, config) => ({ | ||
| config, | ||
| setMultiModalOption: jest.fn() | ||
| })) | ||
| })) | ||
|
|
||
| import { getCredentialData, getCredentialParam } from '../../../src/utils' | ||
| import { ChatOpenAI } from '../ChatOpenAI/FlowiseChatOpenAI' | ||
|
|
||
| const { nodeClass: ChatLitellm } = require('./ChatLitellm') | ||
|
|
||
| describe('ChatLitellm', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks() | ||
| ;(getCredentialData as jest.Mock).mockResolvedValue({}) | ||
| }) | ||
|
|
||
| it('initializes with basic config (API key and model only)', async () => { | ||
| ;(getCredentialParam as jest.Mock).mockImplementation((key: string) => { | ||
| if (key === 'litellmApiKey') return 'sk-test-key' | ||
| return undefined | ||
| }) | ||
|
|
||
| const node = new ChatLitellm() | ||
| const model = await node.init( | ||
| { | ||
| credential: 'cred-1', | ||
| inputs: { | ||
| basePath: 'https://litellm.example.com', | ||
| modelName: 'anthropic/claude-sonnet-4-20250514', | ||
| temperature: '0.7', | ||
| streaming: true | ||
| } | ||
| }, | ||
| '', | ||
| {} | ||
| ) | ||
|
|
||
| expect(ChatOpenAI).toHaveBeenCalledWith( | ||
| undefined, | ||
| expect.objectContaining({ | ||
| modelName: 'anthropic/claude-sonnet-4-20250514', | ||
| temperature: 0.7, | ||
| streaming: true, | ||
| openAIApiKey: 'sk-test-key', | ||
| apiKey: 'sk-test-key', | ||
| configuration: { | ||
| baseURL: 'https://litellm.example.com' | ||
| } | ||
| }) | ||
| ) | ||
| expect(model.setMultiModalOption).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('passes custom headers from credential when provided', async () => { | ||
| ;(getCredentialParam as jest.Mock).mockImplementation((key: string) => { | ||
| if (key === 'litellmApiKey') return 'sk-test-key' | ||
| if (key === 'litellmCustomHeaders') return '{"x-litellm-tags": "team:activation,env:prod"}' | ||
| return undefined | ||
| }) | ||
|
|
||
| const node = new ChatLitellm() | ||
| await node.init( | ||
| { | ||
| credential: 'cred-1', | ||
| inputs: { | ||
| basePath: 'https://litellm.example.com', | ||
| modelName: 'gpt-4o', | ||
| temperature: '0.9', | ||
| streaming: true | ||
| } | ||
| }, | ||
| '', | ||
| {} | ||
| ) | ||
|
|
||
| expect(ChatOpenAI).toHaveBeenCalledWith( | ||
| undefined, | ||
| expect.objectContaining({ | ||
| configuration: { | ||
| baseURL: 'https://litellm.example.com', | ||
| defaultHeaders: { | ||
| 'x-litellm-tags': 'team:activation,env:prod' | ||
| } | ||
| } | ||
| }) | ||
| ) | ||
| }) | ||
|
|
||
| it('ignores malformed custom headers JSON gracefully', async () => { | ||
| ;(getCredentialParam as jest.Mock).mockImplementation((key: string) => { | ||
| if (key === 'litellmApiKey') return 'sk-test-key' | ||
| if (key === 'litellmCustomHeaders') return 'not-valid-json' | ||
| return undefined | ||
| }) | ||
|
|
||
| const node = new ChatLitellm() | ||
| await node.init( | ||
| { | ||
| credential: 'cred-1', | ||
| inputs: { | ||
| basePath: 'https://litellm.example.com', | ||
| modelName: 'gpt-4o', | ||
| temperature: '0.9', | ||
| streaming: true | ||
| } | ||
| }, | ||
| '', | ||
| {} | ||
| ) | ||
|
|
||
| expect(ChatOpenAI).toHaveBeenCalledWith( | ||
| undefined, | ||
| expect.objectContaining({ | ||
| configuration: { | ||
| baseURL: 'https://litellm.example.com' | ||
| } | ||
| }) | ||
| ) | ||
| }) | ||
|
|
||
| it('ignores non-object JSON values (array, string, number)', async () => { | ||
| for (const badValue of ['["a","b"]', '"just-a-string"', '42']) { | ||
| jest.clearAllMocks() | ||
| ;(getCredentialData as jest.Mock).mockResolvedValue({}) | ||
| ;(getCredentialParam as jest.Mock).mockImplementation((key: string) => { | ||
| if (key === 'litellmApiKey') return 'sk-test-key' | ||
| if (key === 'litellmCustomHeaders') return badValue | ||
| return undefined | ||
| }) | ||
|
|
||
| const node = new ChatLitellm() | ||
| await node.init( | ||
| { | ||
| credential: 'cred-1', | ||
| inputs: { | ||
| basePath: 'https://litellm.example.com', | ||
| modelName: 'gpt-4o', | ||
| temperature: '0.9', | ||
| streaming: true | ||
| } | ||
| }, | ||
| '', | ||
| {} | ||
| ) | ||
|
|
||
| const callArgs = (ChatOpenAI as unknown as jest.Mock).mock.calls[0][1] | ||
| expect(callArgs.configuration).toEqual({ baseURL: 'https://litellm.example.com' }) | ||
| } | ||
| }) | ||
|
|
||
| it('works without custom headers (backward compatible)', async () => { | ||
| ;(getCredentialParam as jest.Mock).mockImplementation((key: string) => { | ||
| if (key === 'litellmApiKey') return 'sk-test-key' | ||
| return undefined | ||
| }) | ||
|
|
||
| const node = new ChatLitellm() | ||
| await node.init( | ||
| { | ||
| credential: 'cred-1', | ||
| inputs: { | ||
| basePath: 'https://litellm.example.com', | ||
| modelName: 'gpt-4o', | ||
| temperature: '0.9', | ||
| streaming: true | ||
| } | ||
| }, | ||
| '', | ||
| {} | ||
| ) | ||
|
|
||
| expect(ChatOpenAI).toHaveBeenCalledWith( | ||
| undefined, | ||
| expect.objectContaining({ | ||
| configuration: { | ||
| baseURL: 'https://litellm.example.com' | ||
| } | ||
| }) | ||
| ) | ||
| }) | ||
|
|
||
| it('works without basePath or custom headers', async () => { | ||
| ;(getCredentialParam as jest.Mock).mockImplementation((key: string) => { | ||
| if (key === 'litellmApiKey') return 'sk-test-key' | ||
| return undefined | ||
| }) | ||
|
|
||
| const node = new ChatLitellm() | ||
| await node.init( | ||
| { | ||
| credential: 'cred-1', | ||
| inputs: { | ||
| modelName: 'gpt-4o', | ||
| temperature: '0.5', | ||
| streaming: false | ||
| } | ||
| }, | ||
| '', | ||
| {} | ||
| ) | ||
|
|
||
| const callArgs = (ChatOpenAI as unknown as jest.Mock).mock.calls[0][1] | ||
| expect(callArgs.configuration).toBeUndefined() | ||
| }) | ||
|
|
||
| it('passes optional parameters when provided', async () => { | ||
| ;(getCredentialParam as jest.Mock).mockImplementation((key: string) => { | ||
| if (key === 'litellmApiKey') return 'sk-test-key' | ||
| return undefined | ||
| }) | ||
|
|
||
| const node = new ChatLitellm() | ||
| await node.init( | ||
| { | ||
| credential: 'cred-1', | ||
| inputs: { | ||
| basePath: 'https://litellm.example.com', | ||
| modelName: 'gpt-4o', | ||
| temperature: '0.5', | ||
| streaming: true, | ||
| maxTokens: '4096', | ||
| topP: '0.95', | ||
| timeout: '30000' | ||
| } | ||
| }, | ||
| '', | ||
| {} | ||
| ) | ||
|
|
||
| expect(ChatOpenAI).toHaveBeenCalledWith( | ||
| undefined, | ||
| expect.objectContaining({ | ||
| maxTokens: 4096, | ||
| topP: 0.95, | ||
| timeout: 30000 | ||
| }) | ||
| ) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
customHeadersRawis already parsed or passed as an object (for example, when overridden via API/workflows or if the credential field is populated as an object), callingJSON.parseon it will throw an error and cause the headers to be silently ignored. To make this robust, we should check ifcustomHeadersRawis already an object before attempting to parse it as a string.