Skip to main content
Screenshot 2025-01-05 at 4 45 45 PM

Grok Community XDK

A community-maintained SDK (Standard Developer Kit) for XAI Grok models supporting usage with the Python Programming Language. This library provides convenient access to the xAI REST API from Python 3.11+ applications, with type definitions for all request parameters and response fields.

Installation

pip install xai-grok

Requirements

  • Python 3.11 or higher
  • pydantic
  • requests

Usage

from xai_grok import Grok

client = Grok(
    api_key="your-api-key-here"
)

# Example: Create a chat completion
response = client.chat_completions(
    ChatRequest(
        messages=[
            {"role": "user", "content": "Tell me about AI"}
        ],
        model="model-name-here"
    )
)
print(response.choices[0].message.content)

Available Endpoints

API Key Operations

  • api_key() - Retrieve information about the current API key

Chat and Completion Operations

  • chat_completions(request: ChatRequest) - Create chat completions

    • Input: ChatRequest object containing messages and model settings
    • Throws: InvalidRequestError, IncompleteRequestError
  • complete(request: CompleteRequest) - Generate completions

    • Input: CompleteRequest object with prompt and settings
    • Throws: InvalidRequestError, IncompleteRequestError
  • completions(request: SampleRequest) - Alternative completion endpoint

    • Input: SampleRequest object
    • Throws: InvalidRequestError, IncompleteRequestError

Embedding Operations

  • embedding_model(model_id: str) - Get details of a specific embedding model

    • Input: Model ID string
    • Throws: ModelNotFoundError
  • embedding_models() - List all available embedding models

  • embeddings(request: EmbeddingRequest) - Generate embeddings

    • Input: EmbeddingRequest object
    • Throws: InvalidRequestError, IncompleteRequestError

Language Model Operations

  • language_model(model_id: str) - Get details of a specific language model

    • Input: Model ID string
    • Throws: ModelNotFoundError
  • language_models() - List all available language models

Message Operations

  • messages(request: MessageRequest) - Send messages
    • Input: MessageRequest object
    • Throws: InvalidRequestError, IncompleteRequestError

Model Management

  • models() - List all available models

  • model(model_id: str) - Get details of a specific model

    • Input: Model ID string
    • Throws: ModelNotFoundError

Text Operations

  • tokenize_text(request: TokenizeTextRequest) - Tokenize input text
    • Input: TokenizeTextRequest object
    • Throws: InvalidRequestError

Request and Response Types

All request and response types are Pydantic models, providing type safety and validation. Refer to the schemas module for detailed type definitions.

Base URL

The API uses https://api.x.ai as the base URL for all endpoints.

Authentication

The API requires an authorized API key. Authentication-related errors are handled by specific error types:

  • NoAPIKeyProvidedError: Raised when no API key is provided in the Authorization header
  • InvalidAPIKeyProvidedError: Raised when an incorrect API key is provided

To avoid these errors:

  1. Obtain a valid API key from console.x.ai
  2. Include it in the client initialization:
client = Grok(api_key="your-api-key-here")

Note on Responses

All responses are parsed into their corresponding Pydantic models, providing type-safe access to response data. If the API response cannot be parsed into the expected type, a FailedToParseResponseError will be raised.

Metadata

Release files for xai-grok 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for xai-grok 0.0.1
File Size Uploaded Provenance Actions
xai_grok-0.0.1.tar.gz 18.1 kB Without provenance Details for xai_grok-0.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for xai-grok 0.0.1
File Interpreter ABI Platform Actions
xai_grok-0.0.1-py3-none-any.whl Python 3 none any Details for xai_grok-0.0.1-py3-none-any.whl

Total release size: 47.6 kB

Release files / xai_grok-0.0.1.tar.gz

Download URL xai_grok-0.0.1.tar.gz
Size 18.1 kB
Tags Source
SHA-256 checksum
How to use checksums
81cf1753abff558b5fbe2e57dcfe66d00479c68ae958bb3a66d1fcaefa8f9794
BLAKE2b-256 checksum
How to use checksums
63f97298acdc2fd3bcc93338b52f256ac56c7b34d3447fe9fe31e071a62850c5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.6

Release files / xai_grok-0.0.1-py3-none-any.whl

Download URL xai_grok-0.0.1-py3-none-any.whl
Size 29.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
78eaacdc959bd6b9bb6403f90d70654c97f0f78b7f5fbb2d44c176f415aedfb8
BLAKE2b-256 checksum
How to use checksums
b5ca24af581d53a1764d5a06e6db1e85f7533f0dee65490913f052000fd29a2f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.6

Security for xai-grok 0.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page