Skip to content

How to use dynamic api keys with openAI #2731

Closed
@tzcsx

Description

@tzcsx

I am trying to use Spring AI in a multi-tenant application. The openAI api key needs to be set on a per request basis according to our user context. I think this functionality was added in #2164 however the documentation uses the OpenAiChatClient class which I cannot find in milestone 7 (appears to be gone after 0.8.1?). How can I set the api key for chat and embedding requests?

Activity

markpollack

markpollack commented on Apr 17, 2025

@markpollack
Member

Yikes, many mistakes on my part here, apologies.

The docs are wrong, there was never such a class. The OpenAiChatAutoConfiguration is not allowing one to override the use of SimpleApiKey.

Unfortunately, the @Bean definition that creates OpenAiChatModel contains many arguments, but that would seem to be the best way to add in your own implementation of the ApiKey interface, by basically reproducing what is here

I realize that isn't convenient. I think that the OpenAiChatModel should have some sort mutate() method (like in RestClient) so that you can get the autoconfigured one and then change a few details such as the implementation of the ApiKey interface used.

added this to the 1.0.0-RC1 milestone on Apr 17, 2025
self-assigned this
on Apr 17, 2025
tzcsx

tzcsx commented on Apr 22, 2025

@tzcsx
Author

Thanks for the reply. A mutate() style pre-initialized builder would be just great.

markpollack

markpollack commented on May 9, 2025

@markpollack
Member

#3037 Should help, we are merging it into RC1. Closing this issue, please reopen if that solution doens't work for you. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @markpollack@tzcsx

      Issue actions

        How to use dynamic api keys with openAI · Issue #2731 · spring-projects/spring-ai