Skip to main content

Kubiya SDK

Project description

Kubiya SDK

Kubiya SDK is a powerful Python library for creating, managing, and executing workflows and tools. It provides a flexible and intuitive interface for defining complex workflows, integrating various tools, and managing their execution.

Table of Contents

Installation

To install the Kubiya SDK, use pip:

pip install kubiya-sdk

Quick Start

Here's a simple example to get you started with Kubiya SDK:

from kubiya_sdk.workflows import StatefulWorkflow
from kubiya_sdk.tools import register_tool

# Define a simple tool
@register_tool(name="Multiplier", description="Multiplies a number by 2")
def multiply_by_two(number: int) -> int:
    return number * 2

# Create a workflow
workflow = StatefulWorkflow("SimpleWorkflow")

@workflow.step("step1")
def step1(state):
    return {"result": state["input"] * 2}

@workflow.step("step2")
def step2(state):
    return {"final_result": state["result"] + 1}

workflow.add_edge("step1", "step2")

# Run the workflow
result = await workflow.run({"input": 5})
print(result)

Key Concepts

  • Workflows: Sequences of steps that process and transform data.
  • Steps: Individual units of work within a workflow.
  • Tools: Reusable functions that can be integrated into workflows.
  • State: The data passed between steps in a workflow.

Creating Workflows

Workflows in Kubiya SDK are created using the StatefulWorkflow class:

from kubiya_sdk.workflows import StatefulWorkflow

workflow = StatefulWorkflow("MyWorkflow")

@workflow.step("step_name")
def step_function(state):
    # Process state and return new state
    return {"new_key": "new_value"}

# Connect steps
workflow.add_edge("step1", "step2")

Conditional Workflows

You can add conditions to your workflows:

workflow.add_condition("step1", "state['result'] > 10", "step3")

Defining Tools

Tools are defined using the @register_tool decorator:

from kubiya_sdk.tools import register_tool

@register_tool(name="MyTool", description="Description of my tool")
def my_tool(arg1: int, arg2: str) -> dict:
    # Tool logic here
    return {"result": arg1 + len(arg2)}

Executing Workflows

Workflows can be executed asynchronously:

result = await workflow.run({"input": "initial_value"})

Visualization

Kubiya SDK provides Mermaid diagram generation for workflows:

mermaid_diagram = workflow.to_mermaid()
print(mermaid_diagram)

This will generate a Mermaid diagram string that can be rendered as follows:

graph TD
    step1["🚀 Step 1"] -->|"Result > 10"| step3["🔧 Step 3"]
    step1 -->|"Result <= 10"| step2["🔧 Step 2"]
    step2 --> END["🏁 End"]
    step3 --> END
    classDef startStyle fill:#A2C4C9,stroke:#005F73,stroke-width:2px,color:#000000;
    classDef endStyle fill:#FFB6B9,stroke:#D7263D,stroke-width:2px,color:#000000;
    classDef stepStyle fill:#EDEDED,stroke:#495057,stroke-width:2px,color:#000000;
    class step1 startStyle;
    class END endStyle;

API Reference

For detailed API documentation, please refer to our API Reference.

Examples

Complex Workflow Example

Here's an example of a more complex workflow that demonstrates various features of the Kubiya SDK:

from kubiya_sdk.workflows import StatefulWorkflow
from kubiya_sdk.tools import register_tool

@register_tool(name="DataFetcher", description="Fetches data from an API")
async def fetch_data(api_url: str) -> dict:
    # Simulated API call
    return {"data": f"Data from {api_url}"}

@register_tool(name="DataProcessor", description="Processes fetched data")
def process_data(data: str) -> dict:
    return {"processed_data": f"Processed: {data}"}

workflow = StatefulWorkflow("ComplexWorkflow")

@workflow.step("fetch_step")
async def fetch_step(state):
    tool = workflow.get_tool("DataFetcher")
    result = await tool.execute(api_url=state["api_url"])
    return {"fetched_data": result["data"]}

@workflow.step("process_step")
def process_step(state):
    tool = workflow.get_tool("DataProcessor")
    result = tool.execute(data=state["fetched_data"])
    return {"processed_data": result["processed_data"]}

@workflow.step("decision_step")
def decision_step(state):
    data_length = len(state["processed_data"])
    return {"data_length": data_length}

@workflow.step("short_data_step")
def short_data_step(state):
    return {"result": f"Short data: {state['processed_data']}"}

@workflow.step("long_data_step")
def long_data_step(state):
    return {"result": f"Long data: {state['processed_data'][:50]}..."}

workflow.add_edge("fetch_step", "process_step")
workflow.add_edge("process_step", "decision_step")
workflow.add_condition("decision_step", "state['data_length'] < 50", "short_data_step")
workflow.add_condition("decision_step", "state['data_length'] >= 50", "long_data_step")

# Execution
result = await workflow.run({"api_url": "https://api.example.com/data"})
print(result)

This example demonstrates:

  • Tool registration and usage
  • Multiple workflow steps
  • Conditional branching based on state
  • Asynchronous operations

Contributing

We welcome contributions to the Kubiya SDK! Please see our Contributing Guidelines for more information on how to get started.

License

Kubiya SDK is released under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kubiya_sdk-0.1.5.tar.gz (60.3 kB view details)

Uploaded Source

Built Distribution

kubiya_sdk-0.1.5-py3-none-any.whl (42.4 kB view details)

Uploaded Python 3

File details

Details for the file kubiya_sdk-0.1.5.tar.gz.

File metadata

  • Download URL: kubiya_sdk-0.1.5.tar.gz
  • Upload date:
  • Size: 60.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.4.22

File hashes

Hashes for kubiya_sdk-0.1.5.tar.gz
Algorithm Hash digest
SHA256 76c9f4b031c2e0e31b2edb119d5f0bf8624ee3a34e00046f3b20d51180a56d74
MD5 380787d9cdfb216a58b190e8fb401fc9
BLAKE2b-256 2872e75ea37f3b5857cc811687cb608e9343d1be7d18ddc915b84a0e6b08ce6f

See more details on using hashes here.

File details

Details for the file kubiya_sdk-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for kubiya_sdk-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 a7075765acd5e883b52ea391dcb9615677a6c4dc611aa29a4a32add70698aa13
MD5 82c8e599c4c7a2c8c24b12f3f503049a
BLAKE2b-256 4b3eb4dc0197fd6188e1ed6810ccaf4384cea6062c5e607fc49f575ab6e5671f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page