-
Notifications
You must be signed in to change notification settings - Fork 1.5k
XaiModel with tests and stock analysis agent #3400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
brightsparc
wants to merge
61
commits into
pydantic:main
Choose a base branch
from
brightsparc:xai-sdk
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
61 commits
Select commit
Hold shift + click to select a range
311395d
Initial commit for xAI grok model
brightsparc afd8a4e
Adding flight booking example for live test
brightsparc ab2ff90
Merge remote-tracking branch 'upstream/main' into xai-sdk
brightsparc 4513c8f
Fix pre-commit issues
brightsparc e60d3ea
Updated grok to support passing in AsyncClient, added initial tests
brightsparc 43e6890
Fix pyright
brightsparc 4f15966
Update grok all available grok models
brightsparc 185a6be
Adding image tests that require an active API_KEY
brightsparc a632cbf
Adding MCP added stock analysis agent
brightsparc eb10f47
Merge remote-tracking branch 'upstream/main' into xai-sdk
brightsparc 18b21a5
Updating docs
brightsparc b417485
Set provider response id
brightsparc 0f1c113
Adding reasoning and usage
brightsparc 2b58f59
Update stock analytics agent to stream responses
brightsparc 735da97
Merge remote-tracking branch 'upstream/main' into xai-sdk
brightsparc 65bb646
Remove new line for tool calls
brightsparc b19479e
Adding XAI_API_KEY to test_examples.py
brightsparc 8268267
Adding grok-4-1-fast-non-reasoning as default
brightsparc 843f588
Update to mention GrokModelName in docs
brightsparc 99ad3aa
Renmae GrokModel to XaiModel, and update dependencies and tests. Addβ¦
brightsparc d933b2c
Remove use of hasattr
brightsparc fe93dad
Fix RetryPromptPart
brightsparc 5224e56
Add support for DocumentUrl, and downloading an dummy pdf with casette
brightsparc 128c05a
Implement first pass of response parts using openai chat completions β¦
brightsparc b35bcfd
Adding support for formatting builtin tools
brightsparc 59bf2b6
Update docs for xAI refence
brightsparc e4ebcca
Minor tweak to tidy process response
brightsparc f69a730
Adding prepare_request
brightsparc 052086b
Updating live tests to use mocks, with a design document on the ratioβ¦
brightsparc 4521909
Reduce duplication between request and request_stream, by creating a β¦
brightsparc c9326e1
Adding support for system instructions
brightsparc a53badc
Using system for provider_name, passing in response.timestamp insteadβ¦
brightsparc 26744cc
Add built in tool name and finish reason map
brightsparc ca70b96
Fix tests
brightsparc 999eb81
Adding tool_choice and response_format arguments to create_chat
brightsparc c335ca6
Updating docs for xai.md
brightsparc bcc7834
Update case of xAI
brightsparc 9d06c87
Adding tests for tool_choice and native json output
brightsparc df20aab
Updating docs link for models
brightsparc 656eb62
Create XaiModelName and deprecated GrokProvider
brightsparc 34035c0
Update builtin tests to verify output parts with snapshot
brightsparc 5452ec1
Fix mcp_server tool with prefix and snapshot
brightsparc bf6d4fd
Adding streaming tests for web_search and code_execution
brightsparc da5a14f
Update remaining test to use snapshot
brightsparc 99c83f6
Adding tests for run_stream_events and added constants for model names
brightsparc fb027c2
Merge from main
brightsparc 9ceb5df
Updating documentation for stock_analysis
brightsparc 88f49af
Updating to unlress xai-sdk version
brightsparc 1faabbc
Addressing comments for tool output
brightsparc f91affc
Concat text parts without new lines
brightsparc f163ad6
Concat reasoning parts without new lines
brightsparc cb83948
Fix xai mappings
brightsparc 5656149
Update map response parts for mcp
brightsparc ecffc4c
Refactor tool calling
brightsparc 5f57853
Fix to clean up builtin tool calls
brightsparc 3d3a533
Merge from main
brightsparc 4440dac
Remove redundant assertions
brightsparc 5894cc8
Update to return text part ahead of tool return partn
brightsparc 7400a13
Return empty dict for default tool call output
brightsparc f653e3d
Adding encrypted contents to assistant message
brightsparc 167b44e
Fix pyright
brightsparc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # `pydantic_ai.models.xai` | ||
|
|
||
| ## Setup | ||
|
|
||
| For details on how to set up authentication with this model, see [model configuration for xAI](../../models/xai.md). | ||
|
|
||
| ::: pydantic_ai.models.xai |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| Example of using xAI's server-side web_search tool for stock analysis. | ||
|
|
||
| This example demonstrates: | ||
|
|
||
| - Using server-side tools with `XaiModel` | ||
| - Structured output with Agent | ||
| - Streaming responses | ||
| - Usage tracking with logfire | ||
| - Using built-in and local tools to perform a stock analysis workflow | ||
|
|
||
| In this scenario, the agent uses built-in server-side tools like web_search along with local tools to find the hottest performing stock from yesterday on NASDAQ, retrieve its current price, and provide a brief buy analysis. | ||
|
|
||
| ## Running the Example | ||
|
|
||
| With [dependencies installed and environment variables set](./setup.md#usage), run: | ||
|
|
||
| ```bash | ||
| uv run python -m pydantic_ai_examples.stock_analysis | ||
| ``` | ||
|
|
||
| Note: Requires `XAI_API_KEY` environment variable for xAI API access. | ||
|
|
||
| ## Example Output | ||
|
|
||
| ``` | ||
| π Starting stock analysis... | ||
|
|
||
| Query: What was the hottest performing stock on NASDAQ yesterday? | ||
|
|
||
| π§ Server-side tool: web_search | ||
|
|
||
| π§ Server-side tool: browse_page | ||
|
|
||
| β Analysis complete! | ||
|
|
||
| π Top Stock: ATGL | ||
|
|
||
| π° Current Price: $20.00 | ||
|
|
||
| π Buy Analysis: | ||
| ATGL surged 139.84% on November 18, 2025, making it the hottest performer on NASDAQ, but pulled back 7.45% today to $20.00. The extreme volatility suggests high risk; without clear fundamental drivers, it's not recommended as a buy for most investors. Suitable only for speculative short-term trades. | ||
|
|
||
| π Usage Statistics: | ||
| Requests: 1 | ||
| Input Tokens: 19150 | ||
| Output Tokens: 526 | ||
| Total Tokens: 19676 | ||
| Server-Side Tools: 7 | ||
| ``` | ||
|
|
||
| ## Example Code | ||
|
|
||
| ```snippet {path="/examples/pydantic_ai_examples/stock_analysis.py"}``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| # xAI | ||
|
|
||
| ## Install | ||
|
|
||
| To use [`XaiModel`][pydantic_ai.models.xai.XaiModel], you need to either install `pydantic-ai`, or install `pydantic-ai-slim` with the `xai` optional group: | ||
|
|
||
| ```bash | ||
| pip/uv-add "pydantic-ai-slim[xai]" | ||
| ``` | ||
|
|
||
| ## Configuration | ||
|
|
||
| To use xAI models from [xAI](https://x.ai/api) through their API, go to [console.x.ai](https://console.x.ai/team/default/api-keys) to create an API key. | ||
|
|
||
| [docs.x.ai][https://docs.x.ai/docs/models] contains a list of available xAI models. | ||
|
|
||
| ## Environment variable | ||
|
|
||
| Once you have the API key, you can set it as an environment variable: | ||
|
|
||
| ```bash | ||
| export XAI_API_KEY='your-api-key' | ||
| ``` | ||
|
|
||
| You can then use [`XaiModel`][pydantic_ai.models.xai.XaiModel] by name: | ||
|
|
||
| ```python | ||
| from pydantic_ai import Agent | ||
|
|
||
| agent = Agent('xai:grok-4-1-fast-non-reasoning') | ||
| ... | ||
| ``` | ||
|
|
||
| Or initialise the model directly: | ||
|
|
||
| ```python | ||
| from pydantic_ai import Agent | ||
| from pydantic_ai.models.xai import XaiModel | ||
|
|
||
| # Uses XAI_API_KEY environment variable | ||
| model = XaiModel('grok-4-1-fast-non-reasoning') | ||
| agent = Agent(model) | ||
| ... | ||
| ``` | ||
|
|
||
| You can also customize the [`XaiModel`][pydantic_ai.models.xai.XaiModel] with a custom provider: | ||
|
|
||
| ```python | ||
| from pydantic_ai import Agent | ||
| from pydantic_ai.models.xai import XaiModel | ||
| from pydantic_ai.providers.xai import XaiProvider | ||
|
|
||
| # Custom API key | ||
| provider = XaiProvider(api_key='your-api-key') | ||
| model = XaiModel('grok-4-1-fast-non-reasoning', provider=provider) | ||
| agent = Agent(model) | ||
| ... | ||
| ``` | ||
|
|
||
| Or with a custom `xai_sdk.AsyncClient`: | ||
|
|
||
| ```python | ||
| from xai_sdk import AsyncClient | ||
| from pydantic_ai import Agent | ||
| from pydantic_ai.models.xai import XaiModel | ||
| from pydantic_ai.providers.xai import XaiProvider | ||
|
|
||
| xai_client = AsyncClient(api_key='your-api-key') | ||
| provider = XaiProvider(xai_client=xai_client) | ||
| model = XaiModel('grok-4-1-fast-non-reasoning', provider=provider) | ||
| agent = Agent(model) | ||
| ... | ||
| ``` |
brightsparc marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| """Example of using Grok's server-side web_search tool. | ||
|
|
||
| This agent: | ||
| 1. Uses web_search to find the hottest performing stock yesterday | ||
| 2. Provides buy analysis for the user | ||
| """ | ||
|
|
||
| import logfire | ||
| from pydantic import BaseModel, Field | ||
|
|
||
| from pydantic_ai import ( | ||
| Agent, | ||
| BuiltinToolCallPart, | ||
| RunContext, | ||
| WebSearchTool, | ||
| ) | ||
| from pydantic_ai.models.xai import XaiModel | ||
|
|
||
| logfire.configure() | ||
| logfire.instrument_pydantic_ai() | ||
|
|
||
| # Configure for xAI API - XAI_API_KEY environment variable is required | ||
| # The model will automatically use XaiProvider with the API key from the environment | ||
|
|
||
| # Create the model using XaiModel with server-side tools | ||
| model = XaiModel('grok-4-1-fast-non-reasoning') | ||
|
|
||
|
|
||
| class StockAnalysis(BaseModel): | ||
| """Analysis of top performing stock.""" | ||
|
|
||
| stock_symbol: str = Field(description='Stock ticker symbol') | ||
| performance_change: float = Field( | ||
| description="Yesterday's performance change percentage" | ||
| ) | ||
| current_price: float = Field(description='Current stock price') | ||
| buy_analysis: str = Field(description='Brief analysis for whether to buy the stock') | ||
|
|
||
|
|
||
| # This agent uses server-side web search to research stocks | ||
| stock_analysis_agent = Agent[None, StockAnalysis]( | ||
| model=model, | ||
| output_type=StockAnalysis, | ||
| builtin_tools=[WebSearchTool()], | ||
| system_prompt=( | ||
| 'You are a stock analysis assistant. ' | ||
| 'First, use web_search to find the hottest performing stock from yesterday on NASDAQ, including its symbol and performance change. ' | ||
| 'Then, use the local get_current_price tool to get its current price. ' | ||
| 'Finally, provide a brief buy analysis explaining whether this is a good buy.' | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| @stock_analysis_agent.tool | ||
| def get_current_price(ctx: RunContext, symbol: str) -> float: | ||
| """Get the current price for a stock symbol from local database.""" | ||
| # Simulate local database lookup | ||
| price_db = { | ||
| 'ATGL': 20.00, | ||
| 'EXAMPLE': 150.00, | ||
| # Add more as needed | ||
| } | ||
| return price_db.get(symbol, 0.0) # Return 0 if not found | ||
|
|
||
|
|
||
| async def main(): | ||
| """Run the stock analysis agent.""" | ||
| query = 'What was the hottest performing stock on NASDAQ yesterday?' | ||
|
|
||
| print('π Starting stock analysis...\n') | ||
| print(f'Query: {query}\n') | ||
|
|
||
| async with stock_analysis_agent.run_stream(query) as result: | ||
| # Stream responses as they happen | ||
| async for message, _is_last in result.stream_responses(): | ||
| for part in message.parts: | ||
| if isinstance(part, BuiltinToolCallPart): | ||
| print(f'π§ Server-side tool: {part.tool_name}') | ||
|
|
||
| # Access output after streaming is complete | ||
| output = await result.get_output() | ||
|
|
||
| print('\nβ Analysis complete!\n') | ||
|
|
||
| print(f'π Top Stock: {output.stock_symbol}') | ||
| print(f'π Performance Change: {output.performance_change}%') | ||
| print(f'π° Current Price: ${output.current_price:.2f}') | ||
| print(f'\nπ Buy Analysis:\n{output.buy_analysis}') | ||
|
|
||
| # Show usage statistics | ||
| usage = result.usage() | ||
| print('\nπ Usage Statistics:') | ||
| print(f' Requests: {usage.requests}') | ||
| print(f' Input Tokens: {usage.input_tokens}') | ||
| print(f' Output Tokens: {usage.output_tokens}') | ||
| print(f' Total Tokens: {usage.total_tokens}') | ||
|
|
||
| # Show server-side tools usage if available | ||
| if usage.details and 'server_side_tools_used' in usage.details: | ||
| print(f' Server-Side Tools: {usage.details["server_side_tools_used"]}') | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| import asyncio | ||
|
|
||
| asyncio.run(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,6 +76,7 @@ class WebSearchTool(AbstractBuiltinTool): | |
| * OpenAI Responses | ||
| * Groq | ||
| * xAI | ||
| """ | ||
|
|
||
| search_context_size: Literal['low', 'medium', 'high'] = 'medium' | ||
|
|
@@ -104,6 +105,7 @@ class WebSearchTool(AbstractBuiltinTool): | |
|
|
||
| * Anthropic, see <https://docs.anthropic.com/en/docs/build-with-claude/tool-use/web-search-tool#domain-filtering> | ||
| * Groq, see <https://console.groq.com/docs/agentic-tooling#search-settings> | ||
| * xAI, see <https://docs.x.ai/docs/guides/tools/search-tools#web-search-parameters> | ||
| """ | ||
|
|
||
| allowed_domains: list[str] | None = None | ||
|
|
@@ -115,6 +117,7 @@ class WebSearchTool(AbstractBuiltinTool): | |
|
|
||
| * Anthropic, see <https://docs.anthropic.com/en/docs/build-with-claude/tool-use/web-search-tool#domain-filtering> | ||
| * Groq, see <https://console.groq.com/docs/agentic-tooling#search-settings> | ||
| * xAI, see <https://docs.x.ai/docs/guides/tools/search-tools#web-search-parameters> | ||
| """ | ||
|
|
||
| max_uses: int | None = None | ||
|
|
@@ -160,6 +163,7 @@ class CodeExecutionTool(AbstractBuiltinTool): | |
| * Anthropic | ||
| * OpenAI Responses | ||
| * xAI | ||
| """ | ||
|
|
||
| kind: str = 'code_execution' | ||
|
|
@@ -341,6 +345,7 @@ class MCPServerTool(AbstractBuiltinTool): | |
|
|
||
| * OpenAI Responses | ||
| * Anthropic | ||
| * xAI | ||
| """ | ||
|
|
||
| id: str | ||
|
|
@@ -359,6 +364,7 @@ class MCPServerTool(AbstractBuiltinTool): | |
|
|
||
| * OpenAI Responses | ||
| * Anthropic | ||
| * xAI | ||
| """ | ||
|
|
||
| description: str | None = None | ||
|
|
@@ -367,6 +373,7 @@ class MCPServerTool(AbstractBuiltinTool): | |
| Supported by: | ||
|
|
||
| * OpenAI Responses | ||
| * xAI | ||
| """ | ||
|
|
||
| allowed_tools: list[str] | None = None | ||
|
|
@@ -376,6 +383,7 @@ class MCPServerTool(AbstractBuiltinTool): | |
|
|
||
| * OpenAI Responses | ||
| * Anthropic | ||
| * xAI | ||
| """ | ||
|
|
||
| headers: dict[str, str] | None = None | ||
|
|
@@ -386,6 +394,7 @@ class MCPServerTool(AbstractBuiltinTool): | |
| Supported by: | ||
|
|
||
| * OpenAI Responses | ||
| * xAI | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to update the builtin tools docs as well. Note that I'm also OK moving builtin tool support to a separate PR if that makes things easier. |
||
| """ | ||
|
|
||
| kind: str = 'mcp_server' | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's remove this example