Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/components/credentials/LitellmApi.credential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ class LitellmApi implements INodeCredential {
label: 'API Key',
name: 'litellmApiKey',
type: 'password'
},
{
label: 'Custom Headers',
name: 'litellmCustomHeaders',
type: 'string',
optional: true,
placeholder: '{"x-litellm-tags": "team:activation,env:prod"}'
}
]
}
Expand Down
251 changes: 251 additions & 0 deletions packages/components/nodes/chatmodels/ChatLitellm/ChatLitellm.test.ts
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
})
)
})
})
18 changes: 16 additions & 2 deletions packages/components/nodes/chatmodels/ChatLitellm/ChatLitellm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class ChatLitellm_ChatModels implements INode {

const credentialData = await getCredentialData(nodeData.credential ?? '', options)
const apiKey = getCredentialParam('litellmApiKey', credentialData, nodeData)
const customHeadersRaw = getCredentialParam('litellmCustomHeaders', credentialData, nodeData)

const obj: Partial<OpenAIChatInput> &
BaseLLMParams & { openAIApiKey?: string } & { configuration?: { baseURL?: string; defaultHeaders?: ICommonObject } } = {
Expand All @@ -125,9 +126,22 @@ class ChatLitellm_ChatModels implements INode {
streaming: streaming ?? true
}

if (basePath) {
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
}
}

if (basePath || Object.keys(defaultHeaders).length > 0) {
obj.configuration = {
baseURL: basePath
...(basePath ? { baseURL: basePath } : {}),
...(Object.keys(defaultHeaders).length > 0 ? { defaultHeaders } : {})
}
}

Expand Down