spring-projects / spring-projects/spring-ai

ChatClient returns 401 Invalid API Key when using Groq as OpenAI-compatible provider

Open
#3,289 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage
Dominant language
Java
Stars
9.5k
Forks
2.9k
Avg merge
1d 7h
Merged PRs (30d)
6

Description

Bug description
I'm trying to use [Groq](https://groq.com) as an OpenAI-compatible provider with Spring AI (spring.ai.openai) in version 1.0.0. I have correctly configured the api-key and base-url. The same API key works as expected using curl, but Spring AI consistently returns a 401 - Invalid API Key when invoking ChatClient.


Environment

  • Spring AI version: 1.0.0
  • Spring Boot version: 3.2.5
  • Java version: 17
  • Model: llama3-70b-8192
  • Groq base URL: https://api.groq.com/openai
  • API key format: gsk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (valid and confirmed via curl)
  • No vector store is used

Steps to reproduce

  1. Configure application.yml as below:

    spring:
      ai:
        openai:
          api-key: gsk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
          base-url: https://api.groq.com/openai
          chat:
            options:
              model: llama3-70b-8192
              temperature: 0.7
    
  2. 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();
    }
    
  3. Call the method.


Expected behavior
A valid chat completion response from Groq, the same as received when making the request via curl.


Minimal Complete Reproducible example

Here’s the curl that works:

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.

But with Spring AI, I receive this:

401 - {
  "error": {
    "message": "Invalid API Key",
    "type": "invalid_request_error",
    "code": "invalid_api_key"
  }
}

Please advise if any custom headers or additional configuration is required to support Groq in Spring AI.


Let me know if you'd like to include a GitHub repo or sample project link as well to improve reproducibility.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the ChatClient invocation and the application.yml configuration, then compare the request sent by Spring AI with the working curl request to Groq. Done means the same configuration produces a valid Groq chat completion without the 401 Invalid API Key response.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring, spring-boot
Domain
ai, api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.