Using the decorator on a function

If you want to create a step from a function, you should use the @client.step decorator.

The step is automatically ended and sent to the platform when the function returns.

Another advantage of using the decorator is that you get several variables automatically set for you:

  • name: The name of the step. This is automatically set to the function name.
  • input: The input of the step. This is automatically set to the arguments of the function.
  • output: The output of the step. This is automatically set to the return value of the function.

Here is how to use the decorator:

@client.step
def my_step():
    # do something

If you want to override the default step parameters, you can pass them to the decorator:

@client.step(id="my-step-id", name="My step", type="run")
def my_step():
    # do something

You can access the step object with the get_current_step method:

@client.step
def my_step():
    step = client.get_current_step()
    # do something

Using the with statement

If you want to create a step from a block of code, you should use the with statement.

The step is automatically ended and sent to the platform when the code exits the with block.

with client.step() as step:
    # do something

If you want to override the default step parameters, you can pass them to the step method:

with client.step(id="my-step-id", name="My step", type="run") as step:
    # do something

Using the start_step method

This method should be used as a last resort because it doesn’t automatically end the step.

You must call the end method on the step object to end the step and send it to the platform.

step = client.start_step()
# do something
step.end()

You can either pass the step parameters to the start_step method, or set them directly on the step object:

step = client.start_step(id="my-step-id", name="My step", type="run")
step.input = "test input"
# do something
step.output = "Hello world"
step.end()

Step parameters

thread_id
uuid
required

The id of the thread

id
uuid

The id of the step. If not provided, a random uuid will be generated. Use custom ones to match your own system. Step ids must be unique across your project.

name
string
default: " "

The name of the step (automatically set to the function name if using the decorator)

type
StepType
default: "undefined"

The type of the step. A Step can be one of the following types:

  • run: A generic step
  • tool: A step that runs a tool
  • llm: A step that runs a language model
  • embedding: A step that runs an embedding model
  • retrieval: A step that retrieves documents
  • rerank: A step that reranks documents
  • undefined: An undefined step
metadata
dict
default: "{}"

Metadata associated with the step. This enables you to add custom fields to your steps.

parent_id
uuid

The id of the parent step. This enables you to create nested steps.

start_time
string

The start time of the step.

end_time
string

The end time of the step.

created_at
string

The server-side creation time of the step.

input
dict

A dictionary symbolizing an input. Prefer using content key to store a message.

output
dict

A dictionary symbolizing an output. Prefer using content key to store a message.

tags
List[str]
default: "[]"

The tags of the step. This is a complimentary field to the metadata field. It enables you to add custom tags to your steps.

generation
BaseGeneration

The generation object associated with the step.

feedback
Feedback

The feedback object associated with the step.

attachments
List[Attachment]
default: "[]"

The attachments associated with the step.

Step API

Step API

Manipulate steps directly using the API.