What are Python Transforms?
Python transforms are Python classes that inherit fromInfrahubTransform and implement custom transformation logic. Unlike Jinja2 templates, Python transforms give you:
- Full Python language capabilities
- Access to external libraries and APIs
- Complex data manipulation and business logic
- Type safety and validation
- Structured data output (JSON, YAML, etc.)
- Reusable transformation logic
Creating a Python Transform
1. Define a GraphQL Query
Create a GraphQL query to retrieve the data:2. Create the Transform Class
Create a Python file with a class inheriting fromInfrahubTransform:
3. Register in .infrahub.yml
Add the query and transform to your repository configuration:Transform Class Structure
Basic Structure
Every Python transform follows this pattern:Class Attributes
- query (required): Name or ID of the GraphQL query to execute
- timeout (optional): Maximum execution time in seconds
Transform Method
Thetransform() method receives the GraphQL query response and returns the transformed data:
Data Access Patterns
Raw GraphQL Response (Default)
By default, transforms receive raw GraphQL responses as nested dictionaries:InfrahubNode Objects (Converted)
Setconvert_query_response: true to receive InfrahubNode objects with direct attribute access:
When to Use Each Approach
Raw Response (default):- Simple data extraction
- Working with query results directly
- No need for node operations
convert_query_response: true):
- Complex node operations
- Need to access relationships
- Type-safe attribute access
- Working with InfrahubNode methods
Return Types
Transforms can return various data types:Dictionary (JSON/YAML)
- JSON artifacts (
content_type: application/json) - YAML artifacts (
content_type: application/yaml) - Structured data output
String
- Markdown artifacts (
content_type: text/markdown) - Plain text artifacts (
content_type: text/plain) - Configuration files
List
Real-World Examples
Example 1: Simple Data Extraction
Example 2: Markdown Generation
Example 3: Using Converted Nodes
Example 4: OpenConfig Interface Generation
Transform Execution
Transforms are executed by the Infrahub repository integrator:Using with Artifacts
Combine Python transforms with artifact definitions:- GraphQL query executes with device parameters
- Python transform processes the response
- Return value is formatted based on
content_type - Result is stored as an artifact
Error Handling
Implement proper error handling in transforms:- Missing data in query response
- Unexpected data types
- External API failures
- Timeout exceeded
Accessing the SDK Client
Transforms have access to the Infrahub client:Testing Transforms
Test transforms using pytest:Configuration Options
In .infrahub.yml
Transform Location Format
The transform location uses the formatfile_path::class_name:
Best Practices
-
Type hints: Use type hints for clarity and IDE support
-
Error handling: Handle missing or unexpected data gracefully
-
Docstrings: Document transform purpose and behavior
- Keep transforms focused: One transformation responsibility per class
- Use convert_query_response: When working extensively with nodes
- Set appropriate timeouts: Balance between allowing complexity and preventing hangs
- Test thoroughly: Write unit tests for transform logic
-
Log appropriately: Use
self.logfor debugging information - Validate input: Check data structure before processing
- Return consistent types: Match artifact content_type expectations
Related Topics
- Transformations Overview - Understanding transformation concepts
- Artifacts - Using transforms to generate artifacts
- Jinja2 Templates - Alternative for simple templates
- GraphQL Queries - Writing queries for transforms
Next Steps
- Create your first Python transformation
- Learn about Jinja2 templates for simpler use cases
- Build artifacts using your transforms
- Explore computed attributes with transforms