Skip to content

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

Open
@aminsource

Description

@aminsource

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.

Activity

KevinYe0725

KevinYe0725 commented on May 27, 2025

@KevinYe0725

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      ChatClient returns 401 Invalid API Key when using Groq as OpenAI-compatible provider · Issue #3289 · spring-projects/spring-ai