llm-api-adapter-xai
An optional xAI organization package for LLM API Adapter, a Python SDK with one shared interface for calling LLM APIs.
Uses xAI's official Responses API.
Installation
Install through the core package extra:
pip install "llm-api-adapter[xai]"
Direct installation is also supported when the core package is managed separately:
pip install llm-api-adapter-xai
Async methods need HTTPX:
pip install "llm-api-adapter[xai,async]"
Synchronous requests remains the default. To opt into the HTTPX synchronous
transport, install "llm-api-adapter[xai,httpx]" and pass
transport="httpx".
Quick start
import os
from llm_api_adapter.models.messages.chat_message import UserMessage
from llm_api_adapter.universal_adapter import UniversalLLMAPIAdapter
adapter = UniversalLLMAPIAdapter(
organization="xai",
model="grok-4.7",
api_key=os.environ["XAI_API_KEY"],
)
response = adapter.chat(
messages=[UserMessage("Explain retrieval-augmented generation.")]
)
print(response.content)
Supported models
grok-4.7grok-4.6grok-4.5
Capabilities
| Capability | Supported models |
|---|---|
| Text chat, sync/async streaming, application function tools, JSON Schema/Pydantic output, image URLs or bytes, and PDF URLs or bytes | All three models |
For grok-4.5, grok-4.6, and grok-4.7, xAI cannot disable reasoning: a requested
"none" is mapped to the documented minimum and produces a warning.
grok-4.5 additionally accepts xhigh effort with high behavior. Exact
reasoning values, limits, prices, and exceptions are recorded in the
xAI registry.
Structured-output portability
The package uses the Core portable profile for json_schema and Pydantic
response_model. The main Structured Output guide
defines schema validation and parsed, refusal, and incomplete results.
xAI's documented immediate schema failures are an additive local overlay, not
a replacement for the Core boundary. The adapter rejects boolean property
schemas, empty enum or anyOf, minContains/maxContains, tuple items
arrays, and unsupported regular expressions before the request. See xAI's
structured-output documentation
for xAI-specific details.
Conversations, files, and costs
previous_response is accepted for the shared API, but xAI continuation is
intentionally not used: the adapter does not send previous_response_id.
Keep and provide the complete messages history for each turn, including
assistant tool calls and ToolMessage results.
The package does not expose or send xAI's store option. xAI documents that
Responses are stored server-side by default, so configure data retention with
xAI when that matters to your application. In particular, the library does not
turn on Zero Data Retention (ZDR); enabling ZDR in the xAI Console blocks new
Files API uploads and file_id attachments.
PDF URLs are passed to xAI unchanged. For PDF bytes, the adapter uploads a provider-owned file with a 24-hour expiry; it never deletes or changes a URL or file identifier supplied by your application. No OCR or local text extraction is performed.
Attaching a PDF activates xAI's attachment_search tool. That makes the
request agentic and adds tool-invocation charges to normal token charges.
Storage for an uploaded file is also billed by xAI until it expires. Consult
xAI billing for storage and any charges outside the reported request cost.
See the official xAI documentation for Responses storage, files and expiry, and file-search pricing.
Automatic cache usage and costs
usage.input_tokens_details.cached_tokens becomes Usage.cached_tokens,
a subset of total input. Registry tiers use total input, including cache hits,
and price ordinary and cached input separately. There is no separately priced
automatic cache write; cache_write_tokens remains None. Without the required
cache split, estimated cost_input and cost_total are unknown while known
output cost can remain available. Opt-in cache controls, TTLs, and storage
pricing are outside this estimate.
When xAI reports a valid usage.cost_in_usd_ticks, it takes precedence over
the token estimate: cost_total is that value divided by 10,000,000,000 in USD.
cost_input and cost_output remain None because this total does not itemize
token costs. A known provider total can therefore coexist with unknown input
and output costs. See the shared usage and pricing guide.
See the main llm-api-adapter README for the shared API contract and examples.
Metadata
Release files for llm-api-adapter-xai 0.1.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| llm_api_adapter_xai-0.1.5.tar.gz | 25.5 kB | Without provenance | Details for llm_api_adapter_xai-0.1.5.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| llm_api_adapter_xai-0.1.5-py3-none-any.whl | Python 3 | none | any | Details for llm_api_adapter_xai-0.1.5-py3-none-any.whl |
Total release size: 46.0 kB
Release files / llm_api_adapter_xai-0.1.5.tar.gz
| Download URL | llm_api_adapter_xai-0.1.5.tar.gz |
|---|---|
| Size | 25.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2b6a4bba6972476b6a2c8289e55b77a74c0cea6d9fee670aa43e572ddf00d5b4
|
|
BLAKE2b-256 checksum How to use checksums |
4d8c9f60a08fdab45c74e57f44ff454a6248ec83a1f30f6b55f90029f42c5d3a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Release files / llm_api_adapter_xai-0.1.5-py3-none-any.whl
| Download URL | llm_api_adapter_xai-0.1.5-py3-none-any.whl |
|---|---|
| Size | 20.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
764991453f6826adb0045f0f4426f65c52ef6f8d708a351d3b7203b158216c3a
|
|
BLAKE2b-256 checksum How to use checksums |
c5a19f2fc3c533148022ead2e7e859c2ac4f3748e2ce720ddb0aa451a20d0e0c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Security for llm-api-adapter-xai 0.1.5
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.