xAI plugin
xAI (Grok) is available through the OpenAI-compatible plugin in genkit-openai, including vision models.
Installation
Section titled “Installation”uv add genkit-openaiConfiguration
Section titled “Configuration”Point OpenAI at xAI’s API:
from genkit import Genkitfrom genkit_openai import OpenAIimport os
ai = Genkit( plugins=[ OpenAI( base_url='https://api.x.ai/v1', api_key=os.getenv('XAI_API_KEY'), ), ],)Get an API key from your xAI account settings and pass it as api_key= when you initialize the plugin—for example from the XAI_API_KEY environment variable. Don’t embed API keys directly in code.
Use the openai_model() helper to reference a Grok model.
from genkit import Genkitfrom genkit_openai import OpenAI, openai_modelimport os
ai = Genkit( plugins=[OpenAI(base_url='https://api.x.ai/v1', api_key=os.getenv('XAI_API_KEY'))],)
@ai.flow()async def grok_flow(subject: str) -> str: """Generate a fun fact using Grok.
Args: subject: The subject to generate a fact about.
Returns: A fun fact about the subject. """ response = await ai.generate( model=openai_model('grok-4.3'), prompt=f'tell me a fun fact about {subject}', ) return response.textAdvanced usage
Section titled “Advanced usage”The xAI plugin supports various advanced features for building sophisticated applications.
Available Models:
The xAI plugin provides access to several Grok models:
- Language Models:
grok-4.3(most intelligent and fastest, recommended for chat and coding), plus thegrok-4.20reasoning and multi-agent variants for reasoning and enterprise workloads. See the xAI models documentation for the full current list. - Vision and Image Generation:
grok-4.3provides multimodal vision understanding, andgrok-imagehandles image generation
Tool Calling:
Grok models support tool calling, allowing them to use functions you define:
from pydantic import BaseModel, Field
class WeatherInput(BaseModel): """Input for weather tool.""" location: str = Field(description='City name')
@ai.tool()async def get_weather(input: WeatherInput) -> str: """Get the current weather for a location.""" # In a real implementation, call a weather API return f'The weather in {input.location} is 72°F and sunny.'
response = await ai.generate( model=openai_model('grok-4.3'), prompt="What's the weather like in Austin?", tools=[get_weather],)Streaming:
The plugin supports streaming responses for real-time output:
from genkit import ActionRunContext
@ai.flow()async def streaming_story(name: str, ctx: ActionRunContext) -> str: """Generate a story with streaming output.""" stream_response = ai.generate_stream( model=openai_model('grok-4.3'), prompt=f'Write a short story about {name}', ) async for chunk in stream_response.stream: ctx.send_chunk(chunk.text) return (await stream_response.response).textVision Capabilities:
Use the Grok Vision model to analyze images:
from genkit import Part, TextPart, MediaPart, Media
image_url = 'https://example.com/photo.jpg'
response = await ai.generate( model=openai_model('grok-4.3'), prompt=[ Part(root=TextPart(text='What do you see in this image?')), Part(root=MediaPart(media=Media(url=image_url, content_type='image/jpeg'))), ],)Structured Output:
Generate structured data using Pydantic models:
from pydantic import BaseModel, Field
class MovieRecommendation(BaseModel): """A movie recommendation.""" 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 is recommended')
preferences = 'sci-fi and heist movies'
response = await ai.generate( model=openai_model('grok-4.3'), prompt=f'Recommend a movie for someone who likes: {preferences}', output_schema=MovieRecommendation,)movie = response.output # Typed as MovieRecommendationPassthrough configuration
Section titled “Passthrough configuration”You can pass configuration options that are not defined in the plugin’s custom configuration schema. This permits you to access new models and features without having to update your Genkit version.
from genkit import Genkitfrom genkit_openai import OpenAI, openai_modelimport os
ai = Genkit( plugins=[ OpenAI( base_url='https://api.x.ai/v1', api_key=os.getenv('XAI_API_KEY') ) ],)
response = await ai.generate( prompt='Tell me a cool story', model=openai_model('grok-new'), # hypothetical new model config={ 'new_feature_parameter': ..., # hypothetical config needed for new model },)Genkit passes this configuration as-is to the xAI API giving you access to the new model features. Note that the field name and types are not validated by Genkit and should match the xAI API specification to work.