xAI Plugin
The genkit-plugin-xai package includes a pre-configured plugin for xAI (Grok) models. The XAI plugin provides access to the grok family of models, including vision models.
Installation
Section titled “Installation”uv add genkit-plugin-xaiConfiguration
Section titled “Configuration”To use this plugin, import XAI and specify it when you initialize Genkit:
from genkit import Genkitfrom genkit.plugins.xai import XAI
ai = Genkit( plugins=[XAI()],)You must provide an API key from xAI. You can get an API key from your xAI account settings.
Configure the plugin to use your API key by doing one of the following:
- Set the
XAI_API_KEYenvironment variable to your API key. - Specify the API key when you initialize the plugin:
XAI(api_key='YOUR_API_KEY')As always, avoid embedding API keys directly in your code.
Use the xai_name() helper to reference a Grok model.
from genkit import Genkitfrom genkit.plugins.xai import XAI, xai_name
ai = Genkit( plugins=[XAI()],)
@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=xai_name('grok-3-mini'), 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(most powerful),grok-3,grok-3-fast,grok-3-mini,grok-3-mini-fast - Vision Models:
grok-2-vision-1212for image understanding
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(description='Get current weather for a location')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=xai_name('grok-3-mini'), 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 | None = None) -> str: """Generate a story with streaming output.""" response = await ai.generate( model=xai_name('grok-3'), prompt=f'Write a short story about {name}', on_chunk=ctx.send_chunk if ctx else None, ) return response.textVision Capabilities:
Use the Grok Vision model to analyze images:
from genkit import Part, TextPart, MediaPart, Media
response = await ai.generate( model=xai_name('grok-2-vision-1212'), 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, Fieldfrom genkit import Output
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')
response = await ai.generate( model=xai_name('grok-3-mini'), prompt=f'Recommend a movie for someone who likes: {preferences}', output=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.plugins.xai import XAI, xai_name
ai = Genkit(plugins=[XAI()])
response = await ai.generate( prompt='Tell me a cool story', model=xai_name('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.