Skip to content

Fix/OpenAI sdk api key azure - #5603

Closed
dylanwe wants to merge 1 commit into
spring-projects:mainfrom
dylanwe:fix/openai-sdk-api-key-azure
Closed

Fix/OpenAI sdk api key azure#5603
dylanwe wants to merge 1 commit into
spring-projects:mainfrom
dylanwe:fix/openai-sdk-api-key-azure

Conversation

@dylanwe

@dylanwe dylanwe commented Mar 15, 2026

Copy link
Copy Markdown
Contributor

Hey Spring Team!

I've been using the Open AI SDK for a bit and I noticed that the wrong credentials are used for Azure during auto-configuration. When doing the following in my project:

spring:
  application:
    name: spring-ai
  ai:
    openai-sdk:
      api-key: ${GEN_AI_KEY}
      base-url: https://example/openai/v1
      chat:
        options:
          model: gpt-51
        microsoft-foundry: true

The key is set within the Authorization header but Azure expects it to be set within the api-key header when using an API key and not passwordless auth.

I suggest that when Azure is used and an API key is set we add the AzureApiKeyCredential.

@dylanwe
dylanwe force-pushed the fix/openai-sdk-api-key-azure branch from 3c0c435 to 76b11dc Compare March 15, 2026 14:03
@sdeleuze sdeleuze added this to the 2.0.0-M4 milestone Mar 16, 2026
@sdeleuze sdeleuze self-assigned this Mar 16, 2026
@sdeleuze sdeleuze added bug Something isn't working openai labels Mar 16, 2026
@jdubois

jdubois commented Mar 16, 2026

Copy link
Copy Markdown
Contributor

I've add a look at your comment on ticket #3368 and I believe you are correct, when a key is provided.
Be careful for passwordless: this should still work, when no key is set up.
So I think this should be merged, but the code is starting to be quite complex so I'd like to have some test coverage for this, how about something in OpenAiSdkSetupTests like:

	@Test
	void setupSyncClient_usesApiKeyHeader_notBearerToken_forMicrosoftFoundry() throws Exception {
		OpenAIClient client = OpenAiSdkSetup.setupSyncClient("https://my-resource.openai.azure.com/", "my-foundry-key",
				null, null, null, null, true, false, null, Duration.ofSeconds(30), 2, null, null);

		Field field = client.getClass().getDeclaredField("clientOptions");
		field.setAccessible(true);
		ClientOptions options = (ClientOptions) field.get(client);
		assertInstanceOf(AzureApiKeyCredential.class, options.credential());
		assertThat(options.headers().values("api-key")).containsExactly("my-foundry-key");
		assertThat(options.headers().values("Authorization")).isEmpty();
	}

@dylanwe

dylanwe commented Mar 16, 2026

Copy link
Copy Markdown
Contributor Author

I've add a look at your comment on ticket #3368 and I believe you are correct, when a key is provided. Be careful for passwordless: this should still work, when no key is set up. So I think this should be merged, but the code is starting to be quite complex so I'd like to have some test coverage for this, how about something in OpenAiSdkSetupTests like:

	@Test
	void setupSyncClient_usesApiKeyHeader_notBearerToken_forMicrosoftFoundry() throws Exception {
		OpenAIClient client = OpenAiSdkSetup.setupSyncClient("https://my-resource.openai.azure.com/", "my-foundry-key",
				null, null, null, null, true, false, null, Duration.ofSeconds(30), 2, null, null);

		Field field = client.getClass().getDeclaredField("clientOptions");
		field.setAccessible(true);
		ClientOptions options = (ClientOptions) field.get(client);
		assertInstanceOf(AzureApiKeyCredential.class, options.credential());
		assertThat(options.headers().values("api-key")).containsExactly("my-foundry-key");
		assertThat(options.headers().values("Authorization")).isEmpty();
	}

Thanks for the review and suggestion. I've updated the PR to include the test.

dliubarskyi pushed a commit to langchain4j/langchain4j that referenced this pull request Mar 16, 2026
- Update the Azure SDK to its latest version, to have the latest version
of the Azure Identity library.
- Updated to the latest version of the OpenAi Java SDK
- Modify the algorithm in `OpenAiOfficialSetup` for Microsoft Foundry,
to have a similar implementation as
spring-projects/spring-ai#5603 in Spring AI
@sdeleuze sdeleuze removed their assignment Mar 18, 2026
@ilayaperumalg ilayaperumalg self-assigned this Mar 18, 2026
@ilayaperumalg

Copy link
Copy Markdown
Member

@dylanwe Thanks for the PR! Could you rebase and squash the commits? I am not sure why the commit f8c6c40 is part of this PR

@dylanwe
dylanwe force-pushed the fix/openai-sdk-api-key-azure branch from d17c6fc to 9f42a78 Compare March 18, 2026 16:53
@dylanwe

dylanwe commented Mar 18, 2026

Copy link
Copy Markdown
Contributor Author

@dylanwe Thanks for the PR! Could you rebase and squash the commits? I am not sure why the commit f8c6c40 is part of this PR

@ilayaperumalg oops don't know how that happened, my bad. I've squashed my commits into one.

Signed-off-by: Dylan Weijgertze <dylanwprivate@gmail.com>
@dylanwe
dylanwe force-pushed the fix/openai-sdk-api-key-azure branch from 9f42a78 to bdbebac Compare March 19, 2026 06:27
@ilayaperumalg

Copy link
Copy Markdown
Member

@dylanwe Thanks for the update! Rebased and merged as 0b142aa

Bram-- pushed a commit to Bram--/langchain4j that referenced this pull request Apr 29, 2026
- Update the Azure SDK to its latest version, to have the latest version
of the Azure Identity library.
- Updated to the latest version of the OpenAi Java SDK
- Modify the algorithm in `OpenAiOfficialSetup` for Microsoft Foundry,
to have a similar implementation as
spring-projects/spring-ai#5603 in Spring AI
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working openai

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants