fix(OpenAI Node): Missing header for assistant API calls (#20060)

This commit is contained in:
Eugene
2025-09-26 10:57:58 +02:00
committed by GitHub
parent 677cddab31
commit a6c7e39cb8
2 changed files with 54 additions and 4 deletions
@@ -82,10 +82,11 @@ export class OpenAiApi implements ICredentialType {
credentials: ICredentialDataDecryptedObject,
requestOptions: IHttpRequestOptions,
): Promise<IHttpRequestOptions> {
requestOptions.headers = {
Authorization: 'Bearer ' + credentials.apiKey,
'OpenAI-Organization': credentials.organizationId,
};
requestOptions.headers ??= {};
requestOptions.headers['Authorization'] = `Bearer ${credentials.apiKey}`;
requestOptions.headers['OpenAI-Organization'] = credentials.organizationId;
if (
credentials.header &&
typeof credentials.headerName === 'string' &&
@@ -94,6 +95,7 @@ export class OpenAiApi implements ICredentialType {
) {
requestOptions.headers[credentials.headerName] = credentials.headerValue;
}
return requestOptions;
}
}
@@ -151,5 +151,53 @@ describe('OpenAiApi Credential', () => {
'OpenAI-Organization': '',
});
});
it('should preserve existing headers when adding auth headers', async () => {
const credentials: ICredentialDataDecryptedObject = {
apiKey: 'sk-test123456789',
};
const requestOptions: IHttpRequestOptions = {
headers: {
'OpenAI-Beta': 'assistants=v2',
},
url: '/assistants',
baseURL: 'https://api.openai.com/v1',
};
const result = await openAiApi.authenticate(credentials, requestOptions);
expect(result.headers).toEqual({
'OpenAI-Beta': 'assistants=v2',
Authorization: 'Bearer sk-test123456789',
});
});
it('should preserve existing headers even with custom header option enabled', async () => {
const credentials: ICredentialDataDecryptedObject = {
apiKey: 'sk-test123456789',
header: true,
headerName: 'X-Additional-Header',
headerValue: 'additional-value',
};
const requestOptions: IHttpRequestOptions = {
headers: {
'OpenAI-Beta': 'assistants=v2',
'X-Existing-Header': 'existing-value',
},
url: '/assistants/asst_123',
baseURL: 'https://api.openai.com/v1',
};
const result = await openAiApi.authenticate(credentials, requestOptions);
expect(result.headers).toEqual({
'OpenAI-Beta': 'assistants=v2',
'X-Existing-Header': 'existing-value',
Authorization: 'Bearer sk-test123456789',
'X-Additional-Header': 'additional-value',
});
});
});
});