How to use Groq with OpenAI SDK
Quick answer
Use the OpenAI SDK with the base_url set to Groq's API endpoint https://api.groq.com/openai/v1 and your Groq API key. Call client.chat.completions.create with a Groq model like llama-3.3-70b-versatile and your chat messages to get completions.
PREREQUISITES
Python 3.8+Groq API keypip install openai>=1.0
Setup
Install the official openai Python package and set your Groq API key as an environment variable.
- Run
pip install openaito install the SDK. - Export your API key:
export GROQ_API_KEY='your_api_key_here'on Linux/macOS or set it in your environment variables on Windows.
pip install openai Step by step
Use the OpenAI client with base_url set to Groq's API endpoint. Call chat.completions.create with your model and messages to get a chat completion.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["GROQ_API_KEY"], base_url="https://api.groq.com/openai/v1")
response = client.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=[{"role": "user", "content": "Hello, how are you?"}]
)
print(response.choices[0].message.content) output
I'm doing great, thank you! How can I assist you today?
Common variations
You can enable streaming to receive tokens as they arrive, or use different Groq models by changing the model parameter. The OpenAI SDK supports async calls as well.
import asyncio
import os
from openai import OpenAI
async def main():
client = OpenAI(api_key=os.environ["GROQ_API_KEY"], base_url="https://api.groq.com/openai/v1")
# Async streaming example
stream = await client.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=[{"role": "user", "content": "Tell me a joke."}],
stream=True
)
async for chunk in stream:
print(chunk.choices[0].delta.content or "", end="", flush=True)
if __name__ == "__main__":
asyncio.run(main()) output
Why did the scarecrow win an award? Because he was outstanding in his field!
Troubleshooting
- If you get authentication errors, verify your
GROQ_API_KEYenvironment variable is set correctly. - If you see model not found errors, confirm you are using a valid Groq model name like
llama-3.3-70b-versatile. - For network issues, check your internet connection and firewall settings.
Key Takeaways
- Use the OpenAI SDK with base_url="https://api.groq.com/openai/v1" to access Groq models.
- Set your Groq API key in the environment variable GROQ_API_KEY before running code.
- Call client.chat.completions.create with Groq model names like llama-3.3-70b-versatile for chat completions.
- Enable streaming with stream=True for token-by-token output.
- Check environment variables and model names if you encounter errors.