-
Notifications
You must be signed in to change notification settings - Fork 147
Description
When using Groq with spring.ai.openai I get 401 Invalid API key
I'm trying to use Groq as an OpenAI-compatible provider with Spring AI (spring.ai.openai) in version 1.0.0.
I have correctly set the api-key and base-url, and the same API key works perfectly with curl.
However, when invoking the ChatClient in Spring AI, I consistently receive a 401 - Invalid API Key error from Groq.
Add the following to application.yml:
spring:
ai:
openai:
api-key: gsk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
base-url: https://api.groq.com/openai
chat:
options:
model: llama3-70b-8192
temperature: 0.7
Inject and use ChatClient:
@Autowired
private ChatClient chatClient;
public String callGroq(String prompt) {
return chatClient.prompt()
.system("You are a helpful assistant.")
.user(prompt)
.call()
.content();
}
Result:
401 - {
"error": {
"message": "Invalid API Key",
"type": "invalid_request_error",
"code": "invalid_api_key"
}
}
Groq supports the OpenAI-compatible API, and the same request works using curl:
curl https://api.groq.com/openai/v1/chat/completions -s
-H "Content-Type: application/json"
-H "Authorization: Bearer gsk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
-d '{
"model": "llama3-70b-8192",
"messages": [{"role": "user", "content": "Hello"}]
}'
This returns a valid response with no issue.
Spring AI version: 1.0.0
Boot version: 3.2.5
Model: llama3-70b-8192
Groq’s base URL: https://api.groq.com/openai
I configured spring.ai.openai with my Groq API key and base URL in application.yml. I expected ChatClient to return a valid response from Groq, since the same API key works perfectly with curl. However, Spring AI consistently returns a 401 Invalid API Key error.