Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
aminsource opened this issue May 22, 2025 · 1 comment

Comments

@aminsource
Copy link

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.

@KevinYe0725
Copy link

Try adding the version number /v1/ to the URL part of application.yml .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants