Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Groq Groq python Groq Client Init

From Leeroopedia
Knowledge Sources
Domains API_Client, Authentication
Last Updated 2026-02-15 16:00 GMT

Overview

Concrete tool for establishing authenticated connections to the Groq API provided by the Groq Python SDK.

Description

The Groq class (and its async counterpart AsyncGroq) is the primary entry point for the Groq Python SDK. It extends SyncAPIClient and configures authentication, base URL, timeout, retry policy, and HTTP transport. The client provides lazy-loaded resource accessors for all API namespaces: .chat, .audio, .embeddings, .models, .batches, and .files.

The API key is resolved from the api_key parameter or the GROQ_API_KEY environment variable. If neither is provided, a GroqError is raised.

Usage

Import and instantiate this class at the start of any Groq API workflow. Use Groq for synchronous code and AsyncGroq for asynchronous code. The client instance is reusable across multiple API calls.

Code Reference

Source Location

  • Repository: groq-python
  • File: src/groq/_client.py
  • Lines: L45-247 (Groq), L249-451 (AsyncGroq)

Signature

class Groq(SyncAPIClient):
    def __init__(
        self,
        *,
        api_key: str | None = None,
        base_url: str | httpx.URL | None = None,
        timeout: float | Timeout | None | NotGiven = not_given,
        max_retries: int = DEFAULT_MAX_RETRIES,
        default_headers: Mapping[str, str] | None = None,
        default_query: Mapping[str, object] | None = None,
        http_client: httpx.Client | None = None,
        _strict_response_validation: bool = False,
    ) -> None:
        """
        Construct a new synchronous Groq client instance.
        Automatically infers api_key from GROQ_API_KEY env var if not provided.
        """

Import

from groq import Groq
# For async usage:
from groq import AsyncGroq

I/O Contract

Inputs

Name Type Required Description
api_key str or None No API key for Groq; auto-read from GROQ_API_KEY env var if not provided
base_url str or httpx.URL or None No Override base URL (default: https://api.groq.com)
timeout float or Timeout or None No Request timeout configuration
max_retries int No Maximum retries for failed requests (default: 2)
default_headers Mapping[str, str] or None No Custom headers sent with every request
default_query Mapping[str, object] or None No Custom query params sent with every request
http_client httpx.Client or None No Custom httpx client for advanced transport config

Outputs

Name Type Description
(instance) Groq Configured client with lazy-loaded resource accessors (.chat, .audio, .embeddings, .models, .batches, .files)

Usage Examples

Basic Initialization (Environment Variable)

import os
os.environ["GROQ_API_KEY"] = "gsk_..."

from groq import Groq

# API key auto-read from GROQ_API_KEY
client = Groq()

Explicit API Key

from groq import Groq

client = Groq(api_key="gsk_your_api_key_here")

Custom Configuration

from groq import Groq

client = Groq(
    api_key="gsk_...",
    timeout=30.0,
    max_retries=5,
    base_url="https://custom-proxy.example.com",
)

Async Client

import asyncio
from groq import AsyncGroq

async def main():
    client = AsyncGroq()
    # Use client.chat.completions.create(...) etc.

asyncio.run(main())

Related Pages

Implements Principle

Requires Environment

Uses Heuristics

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment