strands-xai 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,32 @@
1
+ name: Publish to PyPI
2
+
3
+ on:
4
+ release:
5
+ types: [published]
6
+
7
+ permissions:
8
+ contents: read
9
+ id-token: write # Required for trusted publishing
10
+
11
+ jobs:
12
+ build-and-publish:
13
+ runs-on: ubuntu-latest
14
+
15
+ steps:
16
+ - uses: actions/checkout@v4
17
+
18
+ - name: Set up Python
19
+ uses: actions/setup-python@v5
20
+ with:
21
+ python-version: '3.11'
22
+
23
+ - name: Install build tools
24
+ run: |
25
+ python -m pip install --upgrade pip
26
+ pip install build
27
+
28
+ - name: Build package
29
+ run: python -m build
30
+
31
+ - name: Publish to PyPI
32
+ uses: pypa/gh-action-pypi-publish@release/v1
@@ -0,0 +1,37 @@
1
+ name: Tests
2
+
3
+ on:
4
+ push:
5
+ branches: [ main ]
6
+ pull_request:
7
+ branches: [ main ]
8
+
9
+ jobs:
10
+ test:
11
+ runs-on: ubuntu-latest
12
+ strategy:
13
+ matrix:
14
+ python-version: ['3.10', '3.11', '3.12', '3.13', '3.14']
15
+
16
+ steps:
17
+ - uses: actions/checkout@v4
18
+
19
+ - name: Set up Python ${{ matrix.python-version }}
20
+ uses: actions/setup-python@v5
21
+ with:
22
+ python-version: ${{ matrix.python-version }}
23
+
24
+ - name: Install dependencies
25
+ run: |
26
+ python -m pip install --upgrade pip
27
+ pip install -e ".[dev]"
28
+
29
+ - name: Run tests
30
+ run: |
31
+ pytest tests/ -v --cov=strands_xai --cov-report=xml
32
+
33
+ - name: Upload coverage
34
+ uses: codecov/codecov-action@v4
35
+ if: matrix.python-version == '3.11'
36
+ with:
37
+ file: ./coverage.xml
@@ -0,0 +1,54 @@
1
+ # Python
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+ *.so
6
+ .Python
7
+ build/
8
+ develop-eggs/
9
+ dist/
10
+ downloads/
11
+ eggs/
12
+ .eggs/
13
+ lib/
14
+ lib64/
15
+ parts/
16
+ sdist/
17
+ var/
18
+ wheels/
19
+ *.egg-info/
20
+ .installed.cfg
21
+ *.egg
22
+
23
+ # Virtual environments
24
+ venv/
25
+ env/
26
+ ENV/
27
+ .venv
28
+ .env
29
+ .env.local
30
+
31
+ # Testing
32
+ .pytest_cache/
33
+ .coverage
34
+ htmlcov/
35
+ .tox/
36
+
37
+ # IDEs
38
+ .vscode/
39
+ .idea/
40
+ *.swp
41
+ *.swo
42
+ *~
43
+
44
+ # OS
45
+ .DS_Store
46
+ Thumbs.db
47
+
48
+ # Mypy
49
+ .mypy_cache/
50
+ .dmypy.json
51
+ dmypy.json
52
+
53
+ # Ruff
54
+ .ruff_cache/
@@ -0,0 +1,19 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [0.1.0] - 2026-01-23
9
+
10
+ ### Added
11
+ - Initial release of strands-xai
12
+ - Full support for xAI Grok models
13
+ - Server-side tools integration (web_search, x_search, code_execution)
14
+ - Reasoning model support (grok-3-mini with visible reasoning)
15
+ - Encrypted reasoning support (grok-4 multi-turn context)
16
+ - Streaming response support
17
+ - Hybrid tool usage (server-side + client-side tools)
18
+ - Comprehensive unit and integration tests
19
+ - Type hints and mypy support
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Francesco
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,438 @@
1
+ Metadata-Version: 2.4
2
+ Name: strands-xai
3
+ Version: 0.1.0
4
+ Summary: xAI model provider for Strands Agents SDK
5
+ Project-URL: Homepage, https://github.com/Cerrix/strands-xai
6
+ Project-URL: Documentation, https://github.com/Cerrix/strands-xai#readme
7
+ Project-URL: Repository, https://github.com/Cerrix/strands-xai
8
+ Project-URL: Issues, https://github.com/Cerrix/strands-xai/issues
9
+ Author: Francesco Cerizzi
10
+ License-Expression: MIT
11
+ License-File: LICENSE
12
+ Keywords: agents,ai,grok,llm,strands,xai
13
+ Classifier: Development Status :: 4 - Beta
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: License :: OSI Approved :: MIT License
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Programming Language :: Python :: 3.13
21
+ Classifier: Programming Language :: Python :: 3.14
22
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
23
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
24
+ Requires-Python: >=3.10
25
+ Requires-Dist: strands-agents>=1.23.0
26
+ Requires-Dist: xai-sdk<2.0.0,>=1.5.0
27
+ Provides-Extra: dev
28
+ Requires-Dist: build>=0.10.0; extra == 'dev'
29
+ Requires-Dist: mypy>=1.0.0; extra == 'dev'
30
+ Requires-Dist: pytest-asyncio>=0.21.0; extra == 'dev'
31
+ Requires-Dist: pytest-cov>=4.0.0; extra == 'dev'
32
+ Requires-Dist: pytest-mock>=3.0.0; extra == 'dev'
33
+ Requires-Dist: pytest>=7.0.0; extra == 'dev'
34
+ Requires-Dist: ruff>=0.1.0; extra == 'dev'
35
+ Requires-Dist: twine>=4.0.0; extra == 'dev'
36
+ Description-Content-Type: text/markdown
37
+
38
+ # strands-xai
39
+
40
+ [![PyPI version](https://badge.fury.io/py/strands-xai.svg)](https://badge.fury.io/py/strands-xai)
41
+ [![Python Support](https://img.shields.io/pypi/pyversions/strands-xai.svg)](https://pypi.org/project/strands-xai/)
42
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
43
+
44
+ xAI model provider for [Strands Agents SDK](https://github.com/strands-agents/sdk-python)
45
+
46
+ ## Features
47
+
48
+ - **Full Grok Model Support** - Access all xAI Grok models (grok-4, grok-3-mini, etc.)
49
+ - **Server-Side Tools** - Use xAI's built-in tools (web_search, x_search, code_execution)
50
+ - **Reasoning Models** - Leverage visible reasoning (grok-3-mini) or encrypted reasoning (grok-4)
51
+ - **Streaming Support** - Real-time response streaming with full event handling
52
+ - **Hybrid Tool Usage** - Combine xAI server-side tools with Strands client-side tools
53
+ - **Multi-Turn Context** - Seamless conversation history with encrypted content preservation
54
+ - **Type Safe** - Full type hints and mypy support
55
+
56
+ ## Requirements
57
+
58
+ - Python 3.10+
59
+ - Strands Agents SDK 1.23.0+
60
+ - xAI API key from [xAI Console](https://console.x.ai/)
61
+
62
+ ## Installation
63
+
64
+ ```bash
65
+ pip install strands-agents strands-xai
66
+ ```
67
+
68
+ ## Quick Start
69
+
70
+ ### Basic Usage
71
+
72
+ ```python
73
+ from strands_xai import xAIModel
74
+ from strands import Agent
75
+
76
+ # Initialize xAI model
77
+ model = xAIModel(
78
+ client_args={"api_key": "your-xai-api-key"}, # or set XAI_API_KEY env var
79
+ model_id="grok-4-1-fast-non-reasoning-latest",
80
+ )
81
+
82
+ # Create an agent
83
+ agent = Agent(model=model)
84
+
85
+ # Use the agent
86
+ result = agent("What is the capital of France?")
87
+ print(result)
88
+ ```
89
+
90
+ ### With Streaming
91
+
92
+ ```python
93
+ from strands_xai import xAIModel
94
+ from strands import Agent
95
+
96
+ model = xAIModel(
97
+ client_args={"api_key": "your-xai-api-key"},
98
+ model_id="grok-4-1-fast-non-reasoning-latest",
99
+ )
100
+
101
+ agent = Agent(model=model)
102
+
103
+ # Streaming is automatic with Strands callback handlers
104
+ for chunk in agent.stream("Tell me a story"):
105
+ print(chunk, end="", flush=True)
106
+ ```
107
+
108
+ ### With Server-Side Tools
109
+
110
+ ```python
111
+ from strands_xai import xAIModel
112
+ from strands import Agent
113
+ from xai_sdk.tools import x_search, web_search
114
+
115
+ # Use xAI's built-in tools (executed on xAI servers)
116
+ model = xAIModel(
117
+ client_args={"api_key": "your-xai-api-key"},
118
+ model_id="grok-4-1-fast-non-reasoning-latest",
119
+ xai_tools=[x_search(), web_search()],
120
+ )
121
+
122
+ agent = Agent(model=model)
123
+ result = agent("What are people saying about AI on X?")
124
+ print(result)
125
+ ```
126
+
127
+ ### With Reasoning (grok-3-mini)
128
+
129
+ ```python
130
+ from strands_xai import xAIModel
131
+ from strands import Agent
132
+
133
+ # Enable visible reasoning
134
+ model = xAIModel(
135
+ client_args={"api_key": "your-xai-api-key"},
136
+ model_id="grok-3-mini",
137
+ reasoning_effort="high", # "low" or "high"
138
+ )
139
+
140
+ agent = Agent(model=model)
141
+ result = agent("Solve this logic puzzle: If all roses are flowers...")
142
+ print(result)
143
+ ```
144
+
145
+ ### With Encrypted Reasoning (grok-4)
146
+
147
+ For multi-turn conversations with reasoning preserved:
148
+
149
+ ```python
150
+ from strands_xai import xAIModel
151
+ from strands import Agent
152
+
153
+ model = xAIModel(
154
+ client_args={"api_key": "your-xai-api-key"},
155
+ model_id="grok-4-fast-reasoning",
156
+ use_encrypted_content=True, # Preserves reasoning across turns
157
+ )
158
+
159
+ agent = Agent(model=model)
160
+
161
+ # First turn
162
+ result1 = agent("Think through this problem: 2+2")
163
+ print(result1)
164
+
165
+ # Second turn - reasoning context preserved
166
+ result2 = agent("Now multiply that by 3")
167
+ print(result2)
168
+ ```
169
+
170
+ ### With Inline Citations
171
+
172
+ Get sources cited directly in responses:
173
+
174
+ ```python
175
+ from strands_xai import xAIModel
176
+ from strands import Agent
177
+ from xai_sdk.tools import web_search
178
+
179
+ model = xAIModel(
180
+ client_args={"api_key": "your-xai-api-key"},
181
+ model_id="grok-4-1-fast-non-reasoning-latest",
182
+ xai_tools=[web_search()],
183
+ include=["inline_citations"], # Enable citations
184
+ )
185
+
186
+ agent = Agent(
187
+ model=model,
188
+ system_prompt="You are a helpful assistant. Always cite sources."
189
+ )
190
+
191
+ result = agent("What are the latest developments in AI?")
192
+ print(result)
193
+ # Output includes inline citations like [1], [2] with source URLs
194
+ ```
195
+
196
+ ### Hybrid: Server-Side + Client-Side Tools
197
+
198
+ ```python
199
+ from strands_xai import xAIModel
200
+ from strands import Agent, tool
201
+ from xai_sdk.tools import x_search
202
+
203
+ @tool
204
+ def get_weather(city: str) -> str:
205
+ """Get weather for a city."""
206
+ return f"Weather in {city}: Sunny, 22°C"
207
+
208
+ # Combine xAI tools with Strands tools
209
+ model = xAIModel(
210
+ client_args={"api_key": "your-xai-api-key"},
211
+ model_id="grok-4-1-fast-non-reasoning-latest",
212
+ xai_tools=[x_search()],
213
+ )
214
+
215
+ agent = Agent(model=model, tools=[get_weather])
216
+ result = agent("What's the weather in Paris and what are people tweeting about it?")
217
+ print(result)
218
+ ```
219
+
220
+ ## Configuration Options
221
+
222
+ | Parameter | Type | Description |
223
+ |-----------|------|-------------|
224
+ | `model_id` | `str` | Grok model ID (e.g., "grok-4", "grok-3-mini") |
225
+ | `client_args` | `dict` | Arguments for xAI client (api_key, timeout, etc.) |
226
+ | `params` | `dict` | Model parameters (temperature, max_tokens, etc.) |
227
+ | `xai_tools` | `list` | Server-side tools from xai_sdk.tools |
228
+ | `reasoning_effort` | `str` | "low" or "high" (grok-3-mini only) |
229
+ | `use_encrypted_content` | `bool` | Enable encrypted reasoning for multi-turn |
230
+ | `include` | `list` | Optional xAI features (e.g., `["inline_citations"]`) |
231
+
232
+ ### Model Parameters
233
+
234
+ Common parameters you can pass in `params`:
235
+
236
+ ```python
237
+ model = xAIModel(
238
+ client_args={"api_key": "your-xai-api-key"},
239
+ model_id="grok-4-1-fast-non-reasoning-latest",
240
+ params={
241
+ "temperature": 0.7, # 0.0-2.0, controls randomness
242
+ "max_tokens": 2048, # Maximum tokens in response
243
+ "top_p": 0.9, # Nucleus sampling
244
+ "frequency_penalty": 0, # -2.0 to 2.0
245
+ "presence_penalty": 0, # -2.0 to 2.0
246
+ }
247
+ )
248
+ ```
249
+
250
+ ## Available Models
251
+
252
+ | Model | Context | Best For |
253
+ |-------|---------|----------|
254
+ | `grok-4-1-fast-reasoning` | 2M | Fast reasoning with encrypted thinking |
255
+ | `grok-4-1-fast-non-reasoning` | 2M | Fast, high-performance inference |
256
+ | `grok-4-fast-reasoning` | 2M | Reasoning with encrypted thinking |
257
+ | `grok-4-fast-non-reasoning` | 2M | Fast inference without reasoning |
258
+ | `grok-4-0709` | 256K | Premium model (higher cost) |
259
+ | `grok-code-fast-1` | 256K | Code-optimized model |
260
+ | `grok-3-mini` | 131K | Compact with visible reasoning |
261
+
262
+ See [xAI documentation](https://docs.x.ai/) for pricing and rate limits.
263
+
264
+ ## Server-Side Tools
265
+
266
+ xAI provides built-in tools executed on their infrastructure:
267
+
268
+ ### Available Tools
269
+
270
+ - **`web_search()`** - Search the web for current information
271
+ - **`x_search()`** - Search X (Twitter) for posts and trends
272
+ - **`code_execution()`** - Execute Python code safely
273
+
274
+ ### Basic Usage
275
+
276
+ ```python
277
+ from strands_xai import xAIModel
278
+ from strands import Agent
279
+ from xai_sdk.tools import web_search, x_search, code_execution
280
+
281
+ model = xAIModel(
282
+ client_args={"api_key": "your-xai-api-key"},
283
+ model_id="grok-4-1-fast-non-reasoning-latest",
284
+ xai_tools=[web_search(), x_search(), code_execution()],
285
+ )
286
+
287
+ agent = Agent(model=model)
288
+ result = agent("What's trending on X about AI?")
289
+ ```
290
+
291
+ ### Why Server-Side Tools?
292
+
293
+ - ✅ **No implementation needed** - xAI handles execution
294
+ - ✅ **Always up-to-date** - Real-time web/X data
295
+ - ✅ **Secure** - Code execution in sandboxed environment
296
+ - ✅ **Fast** - Optimized by xAI infrastructure
297
+
298
+ ### Combining with Client-Side Tools
299
+
300
+ Mix xAI server-side tools with your own Strands tools:
301
+
302
+ ```python
303
+ from strands_xai import xAIModel
304
+ from strands import Agent, tool
305
+ from xai_sdk.tools import x_search
306
+
307
+ @tool
308
+ def get_weather(city: str) -> str:
309
+ """Get weather for a city."""
310
+ return f"Weather in {city}: Sunny, 22°C"
311
+
312
+ model = xAIModel(
313
+ client_args={"api_key": "your-xai-api-key"},
314
+ model_id="grok-4-1-fast-non-reasoning-latest",
315
+ xai_tools=[x_search()], # Server-side
316
+ )
317
+
318
+ agent = Agent(
319
+ model=model,
320
+ tools=[get_weather] # Client-side
321
+ )
322
+
323
+ # Agent can use both types of tools
324
+ result = agent("What's the weather in Paris and what are people saying about it on X?")
325
+ ```
326
+
327
+ ## Examples
328
+
329
+ See the [examples](examples/) directory for complete working examples.
330
+
331
+ ### Interactive Chat
332
+
333
+ Full-featured interactive chat with 10 different agent configurations:
334
+
335
+ ```bash
336
+ export XAI_API_KEY="your-xai-api-key"
337
+ cd strands-xai
338
+ source .venv/bin/activate
339
+ python examples/interactive_chat.py
340
+ ```
341
+
342
+ Choose from:
343
+ - Simple (non-streaming)
344
+ - Streaming with debug mode
345
+ - Client-side tools (calculator, weather)
346
+ - Server-side tools (X search, web search)
347
+ - Hybrid (both server and client tools)
348
+ - Reasoning models (grok-3-mini, grok-4)
349
+ - Web search with citations
350
+
351
+ ### Quick Test
352
+
353
+ ```bash
354
+ export XAI_API_KEY="your-xai-api-key"
355
+ python examples/test_grok_final.py
356
+ ```
357
+
358
+ Or use the convenience script:
359
+
360
+ ```bash
361
+ ./run_examples.sh chat # Interactive chat
362
+ ./run_examples.sh test # Quick test
363
+ ```
364
+
365
+ ## Development
366
+
367
+ ```bash
368
+ # Clone the repository
369
+ git clone https://github.com/Cerrix/strands-xai.git
370
+ cd strands-xai
371
+
372
+ # Install with dev dependencies
373
+ pip install -e ".[dev]"
374
+
375
+ # Run tests
376
+ pytest
377
+
378
+ # Run tests with coverage
379
+ pytest --cov=strands_xai --cov-report=html
380
+
381
+ # Format code
382
+ ruff format .
383
+
384
+ # Lint code
385
+ ruff check .
386
+
387
+ # Type check
388
+ mypy src/strands_xai
389
+ ```
390
+
391
+ ## Testing
392
+
393
+ ### Unit Tests
394
+
395
+ The package includes 74 comprehensive unit tests:
396
+
397
+ ```bash
398
+ # Run all tests
399
+ pytest
400
+
401
+ # Run with coverage
402
+ pytest --cov=strands_xai --cov-report=html
403
+
404
+ # Run specific test
405
+ pytest tests/test_xai.py::TestBuildChat -v
406
+ ```
407
+
408
+ ### Integration Tests with Real API
409
+
410
+ Test with your xAI API key using the example scripts:
411
+
412
+ ```bash
413
+ export XAI_API_KEY="your-xai-api-key"
414
+
415
+ # Interactive testing
416
+ python examples/interactive_chat.py
417
+
418
+ # Quick functionality test
419
+ python examples/test_grok_final.py
420
+ ```
421
+
422
+ ## Contributing
423
+
424
+ Contributions are welcome! Please feel free to submit a Pull Request.
425
+
426
+ ## License
427
+
428
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
429
+
430
+ ## Support
431
+
432
+ - [GitHub Issues](https://github.com/Cerrix/strands-xai/issues)
433
+ - [Strands Agents Documentation](https://strandsagents.com/)
434
+ - [xAI Documentation](https://docs.x.ai/)
435
+
436
+ ## Acknowledgments
437
+
438
+ Built for the [Strands Agents](https://github.com/strands-agents/sdk-python) community.