xAI plugin
xAI (Grok) models run through the OpenAI-compatible OpenAI plugin in genkit-openai.
Installation
Section titled “Installation”uv add genkit-openaiConfiguration
Section titled “Configuration”Point OpenAI at xAI’s API endpoint and pass your API key from the xAI console:
import osfrom genkit import Genkitfrom genkit_openai import OpenAI
ai = Genkit( plugins=[ OpenAI( base_url='https://api.x.ai/v1', api_key=os.environ['XAI_API_KEY'], ), ],)For multi-tenant apps, override the key on individual calls with context={'secrets': {'api_key': tenant_key}}.
Reference Grok models with the 'openai/<model-id>' prefix:
@ai.flow()async def grok_flow(subject: str) -> str: response = await ai.generate( model='openai/grok-4.3', prompt=f'Give one surprising engineering fact about {subject}.', ) return response.textAvailable models:
grok-4.3: General chat, coding, and image understandinggrok-4.20variants: Reasoning workloads
See the xAI models documentation for the current list.
The Dev UI lists the Grok models it reads from xAI’s /v1/models endpoint, next to the plugin’s built-in OpenAI catalog. Catalog entries such as openai/gpt-4o don’t work against xAI.
Advanced usage
Section titled “Advanced usage”Tool calling
Section titled “Tool calling”from pydantic import BaseModel, Field
class WeatherInput(BaseModel): location: str = Field(description='City name')
@ai.tool()async def get_weather(input: WeatherInput) -> str: """Get the current weather for a location.""" return f'The weather in {input.location} is 72°F and sunny.'
response = await ai.generate( model='openai/grok-4.3', prompt="What's the weather like in Austin?", tools=[get_weather], tool_choice='auto',)print(response.text)# => It's 72°F and sunny in Austin.Streaming
Section titled “Streaming”from genkit import FlowContext
@ai.flow()async def streaming_story(topic: str, ctx: FlowContext[str]) -> str: stream_response = ai.generate_stream( model='openai/grok-4.3', prompt=f'Write a short dispatch about {topic}.', ) async for chunk in stream_response.stream: ctx.send_chunk(chunk.text) return (await stream_response.response).textVision capabilities
Section titled “Vision capabilities”Construct multimodal prompts with Part.from_text() and Part.from_media():
from genkit import Part
image_url = 'https://example.com/diagram.jpg'
response = await ai.generate( model='openai/grok-4.3', prompt=[ Part.from_text('List the services shown in this architecture diagram.'), Part.from_media(url=image_url, content_type='image/jpeg'), ],)Structured output
Section titled “Structured output”Genkit sends your schema to xAI as a json_schema response format. response.output is a validated MovieRecommendation instance.
from pydantic import BaseModel, Field
class MovieRecommendation(BaseModel): title: str = Field(description='Movie title') year: int = Field(description='Release year') genre: str = Field(description='Primary genre') reason: str = Field(description='Why this movie fits the request')
response = await ai.generate( model='openai/grok-4.3', prompt='Recommend a heist film with practical effects.', output_schema=MovieRecommendation,)movie: MovieRecommendation = response.outputPassthrough configuration
Section titled “Passthrough configuration”OpenAIConfig rejects undeclared top-level keys. Pass request fields it does not declare, such as the end-user user ID, inside extra:
from genkit_openai import OpenAIConfig
response = await ai.generate( model='openai/grok-4.3', prompt='Summarize this support thread.', config=OpenAIConfig( temperature=0.3, extra={'user': 'acct_9481'}, ),)