---
title: "Python Code Builder"
icon: "hammer"
---

## Overview

The Python AI Builder is an intelligent coding assistant integrated directly into your Airia agents. It helps you write, debug, and optimize Python code with instant, context-aware assistance, eliminating the need to switch tools or search online.

## Key Capabilities

The Python AI Builder provides four core capabilities to accelerate your Python development:

### Fix Errors in Your Code

The AI Builder analyzes your code, error messages, and execution logs to identify root causes and suggest precise fixes.

**It helps with:**
*   Syntax errors and typos
*   Runtime exceptions and logic errors
*   Type mismatches and validation
*   Variable scope and reference problems
*   Import and dependency errors

The assistant explains the error and provides corrected code you can apply directly.

### Explain Your Code

Get clear, plain-language explanations of your Python code, its purpose, and structure.

**It explains:**
*   Overall code purpose and workflow
*   Individual function and class behavior
*   Complex algorithms and logic patterns
*   Data transformations and processing
*   Library usage and best practices

This is useful for onboarding, reviewing inherited code, or documenting your work.

### Optimize Performance

Improve your code's speed and memory usage with AI-driven optimization suggestions.

**It optimizes:**
*   Algorithm efficiency and complexity
*   Data structure selection
*   Loop and iteration patterns
*   Memory usage and object creation
*   Library and built-in function usage
*   Unnecessary operations and redundancy

You'll receive specific recommendations with explanations of performance benefits and trade-offs.

### Add New Features

Extend your code with new functionality while maintaining quality and consistency.

**It helps build:**
*   Input validation and error handling
*   Data processing and transformation logic
*   Integration with external libraries
*   Conditional logic and business rules
*   Output formatting and structure
*   Utility functions and helper methods

The assistant ensures new code follows best practices and integrates seamlessly.

## Feature Availability

### Check if the AI Builder is Enabled

The Python AI Builder must be enabled by your administrator. If you do not see the **Agentic** tab in your Python code blocks, the feature may not be enabled for your organization.

### Request Access

If the Python AI Builder is not available in your environment:

1.  **Contact your Airia administrator.** Reach out to the team responsible for managing your Airia platform settings.
2.  **Explain your use case.** Share how the AI Builder would benefit your work, such as:
    *   Faster development and debugging
    *   Improved code quality and optimization
    *   Easier onboarding and learning
    *   Reduced time spent searching for solutions
3.  **Reference this documentation.** Direct your administrator to this guide.
4.  **Ask about enablement.** Your administrator can enable the Python AI Builder through the platform settings.

### For Administrators: Enable Python AI Builder

<Accordion title="How to Enable Python AI Builder (Admin Guide)">
  To enable the Python AI Builder for your users:

  1.  Navigate to **Settings** in your Airia platform.
  2.  Look for **Components** > **Build with AI**
  3.  Find the **"Python Code Block Vibe Experience"** setting.
  4.  Toggle it **ON** to enable the feature.

  The Python AI Builder will become available to all users in your organization who work with Python code blocks in agents.
</Accordion>

## Access the Python AI Builder

Once enabled, the Python AI Builder is integrated directly into Python code blocks in your Agent Studio:

1.  **Create** or **open** a Python code block in your agent.
2.  Look for and click the **Agentic** tab next to the code editor.
3.  The Python Coding Assistant interface will open, ready for you to start chatting.

The assistant has full visibility into your current code, recent execution results, and errors, providing complete context for accurate help.

## Use the Python Coding Assistant

### Quick Start with Action Cards

When you open the Python AI Builder, you'll see four action cards:

*   **Fix errors in my code**: Resolve bugs and errors.
*   **Explain this code**: Understand your code's functionality.
*   **Optimize performance**: Improve efficiency and speed.
*   **Add a new feature**: Extend functionality.

Click any card to start a task-focused conversation. The assistant will analyze your code and provide tailored guidance.

### Natural Conversation

You can ask the AI Builder questions about your Python code using natural language. The assistant maintains conversation context, allowing for back-and-forth discussions to refine solutions.

**Example questions:**
*   "Why is this loop running so slowly?"
*   "How can I handle the case where the input is empty?"
*   "Add error handling for API timeouts."
*   "What does this function return when `x` is negative?"
*   "Make this code more readable."
*   "How do I parse JSON from this string?"

### Code Context Awareness

The Python AI Builder automatically understands:

*   **Your current code**: The exact Python code in your editor.
*   **Execution results**: Output from recent test runs.
*   **Error messages**: Full stack traces and error details.
*   **Debug logs**: Any logging output from your code.
*   **Available variables**: Input data and context variables accessible in your code.

You do not need to copy-paste code or errors; the assistant has all necessary context.

### Apply Code Changes

When the AI Builder suggests code changes:

1.  **Review the suggestion.** The assistant explains what changed and why.
2.  **Code updates automatically.** Suggested code appears in your editor in real-time.
3.  **Test the changes.** Click **Run Code** to execute and verify.
4.  **Iterate if needed.** Continue the conversation to refine or adjust.

All code changes are tracked with version history, showing who made each change (you or the AI assistant) and when.

## Execute and Test Your Code

### Run Your Code

Test your Python code directly within the editor using the **Run Code** button. Your code executes in a secure, isolated environment with access to:

*   **Input data**: Output from the previous agent step, available as the `input` variable.
*   **User context**: Information about the current user, roles, and permissions.
*   **Previous results**: Data from earlier steps in your agent.
*   **Custom variables**: Any variables configured in your agent.

### Understand Output

After execution, view results in the tabbed output panel:

*   **Output**: Standard output from your code (e.g., what `print()` displays).
*   **Debug Logs**: Detailed logging for troubleshooting.
*   **Stack Trace**: Error details if execution failed.
*   **Return Code**: Execution status (`0` for success, non-zero for errors).

If your code encounters errors, this information is automatically shared with the AI Builder for more accurate debugging.

### Set Output Variables

Your Python code must assign its final result to the `output` variable:

```python
# Your code logic here
result = process_data(input)

# Assign result to output variable
output = result
```

The `output` variable becomes available to subsequent steps in your agent.

## Code Version History

Every time you or the AI Builder modifies the code, a new version is saved. You can access version history to:

*   **View all changes** over time.
*   **See attribution** for each change (you or the AI).
*   **Restore previous versions** if needed.

This audit trail helps you track code evolution and the impact of AI suggestions.

## Best Practices

*   **Be Specific in Your Requests**: Provide detailed context.
    *   ❌ "Fix this"
    *   ✅ "Fix the `TypeError` on line 15 when `input` is `None`."
*   **Test Frequently**: Run your code after each significant change to verify suggestions, catch errors early, and provide up-to-date execution context.
*   **Iterate and Refine**: Ask follow-up questions to explore alternatives or clarify solutions.
    *   "Can you explain that approach differently?"
    *   "What are the trade-offs with this solution?"
    *   "Is there a simpler way to do this?"
*   **Review AI Suggestions**: Always review suggested code to ensure it meets requirements, handles edge cases, follows coding standards, and is fully understood.

## Common Use Cases

### Debug Data Transformation Errors

**Scenario:** Your code processes JSON data but fails with certain input formats.

**Approach:**
1.  Run your code with the problematic input.
2.  Open the AI Builder and click **Fix errors in my code**.
3.  The assistant analyzes the error and suggests input validation.
4.  Test the fix with various inputs.

### Understand Inherited Code

**Scenario:** You've taken over a agent with complex Python logic you didn't write.

**Approach:**
1.  Open the Python code block.
2.  Ask the AI Builder to **Explain this code**.
3.  Request clarification on specific sections or functions.
4.  Ask, "What would happen if `input` is empty?" to understand edge case handling.

### Improve Slow agent Steps

**Scenario:** A Python step takes too long to process large datasets.

**Approach:**
1.  Click **Optimize performance** in the AI Builder.
2.  Review suggested optimizations (e.g., using list comprehensions, better data structures).
3.  Apply changes and test with your typical data volume.
4.  Compare execution times before and after.

### Add Input Validation

**Scenario:** Your code crashes when users provide unexpected input.

**Approach:**
1.  Ask the AI Builder to **Add input validation for required fields**.
2.  Specify validation rules (type checking, range validation, etc.).
3.  The assistant adds validation logic and error handling.
4.  Test with various valid and invalid inputs.
