If you are new to GAIA API and want to build a stage, we recommend you start with How to Create a Stage


Stages are SINGLE ACTION modules, design to be implemented along side other stages to form a pipeline. There are stages provided out-of-the-box, but most likely is that your project will required a custom stage for a specific step, so before start working on your stage please check the following recommendations

Recommendations

Stage by Stage

Apply your business rules one stage at the time, making the whole process modular and easier to customize, and reuse.

When Possible, Make It as Generic as Possible

Stages modular nature, allows for them to be integrated into the base code, if they are generic enough.

The development team, can add them, and increase the built in options for future projects, taking advantage from the project synergy.




Stage Parts

Configuration

As in all other configurations the stage configuration is a Pydantic model, so all advantages of Pydantic applied to the stage configuration (e.g, validations data transformation)

The BaseStage class is a Pydantic model with general configuration setup, were all base parameters are optional except for name

Class


Object

class DemoStage(BaseStage):
    pass


DemoStage(
    enable=True,
    name='demo',
    save_to_intermediate=False,
    expand_result=False,
    ui_only=None,
    halt_on_exception=False
)

To add parameters, just class variables and their type. In the example below, all parameters are required, the only validation besides required, is their type.

In the following example the default values where omitted, leaving only the new and required values

Class


Object

class DemoStage(BaseStage):
    message: str
    parameter_a: int
    parameter_b: float


DemoStage(
    name='demo',
    message='Add this to response',
    parameter_a=5,     
    parameter_a=3.6
)

Add Pydantic features

All examples above are Pydantic models, but none is taking advantage of it, so if we add this feature we would get something like this

Class


Object

from typing import Optional
from pydantic import Field

class DemoStage(BaseStage):
    message: Optional[str] = Field(title='Title Message',
                                   description='Message to put on the response body')
    parameter_a: int = Field(default=1, ge=0, lt=10,
                             description='Parameter to do something')
    parameter_b: float = Field(..., default=0.5, ge=0, lt=10,
                               description='Parameter to do something else')


DemoStage(
    name='demo',
    message='Add this to response',
    parameter_a=5,     
    parameter_a=3.6
)

Here we are:

  • Adding documentation when putting tittle and description
  • Making message optional, with the Optional typing 
  • Making parameter_a optional by adding a default value
  • Adding restrictions and verification to parameter_a and parameter_b with ge (greater or equal) and lt (lower than)

    If the value set does not met the gt and lt parameter requirements, an exception is thrown

  • Making parameter_b required even if we set a default, meaning only by setting parameter_b as None, will the default by use




Init

Initialization of the stage (constructor), executed only once when the pipeline is build, or when the pipeline is updated.

The init of the stage must always accept a config parameter, with the type of the Stage class.

    def __init__(self, config: DemoStage):
        super().__init__(config)

        import requests

        url = "https://api.example.com/data"  # Replace with your desired URL

        response = requests.get(url)

        if response.status_code == 200:
            print(f"Response: {response.text}")
        else:
            print(f"GET request failed with status code: {response.status_code}")

Useful for:

  • Instance variables setup
  • API calls
  • File downloading & loading
  • ML model initialization
  • One time calculations




Methods

Each stage has 3 required methods, process being the main one, post_process the alternative or secondary executed only after process and get_ui_config currently under development and still not a definitive method

This methods share the same parameters:


self

  • Access to instance variables
  • Access to the configuration via self.config

intermediate

  • Has the body of the request
  • Has field added by other stages
  • Information stored here will not end in the final response
  • Useful for passing data through stages

final

  • Has final response, up to that moment
  • Immutable, can’t add or delete from it (but can modify data already stored)
  • Any return from a stage will end in the final using the stage name as key

request

  • Raw request triggering the pipeline
  • User data, header, query parameter available

Request allows access to the user information currently sending the request

user: User = request.state.user
  • No labels