Skip to main content
Custom functions give you full control over a step’s logic. Use them to preprocess inputs, orchestrate agents and teams, and postprocess outputs. Key Capabilities
  • Custom Logic: Implement complex business rules and data transformations
  • Agent Integration: Call agents and teams within your custom processing logic
  • Data Flow Control: Transform outputs between steps
Implementation Pattern Define a Step with a custom function as the executor. A standard function accepts StepInput and returns StepOutput. Generator functions can yield executor events before yielding their final StepOutput. Custom function step workflow diagram

Example

Standard Pattern A synchronous function step follows this structure:

Class-based executor

You can also use a class-based executor by defining a class that implements the __call__ method.
When is this useful?
  • Configuration at initialization: Pass in settings, API keys, or behavior flags when creating the executor
  • Stateful execution: Maintain state while the same executor instance is reused
  • Reusable components: Create configured executor instances that can be shared across multiple workflows
Protect mutable executor state when workflow runs can overlap.
Class-based executors also support async execution. Define the __call__ method as an async function.
For a detailed example see Class-based Executor.

Streaming from a Custom Function

An async generator function can yield agent or team events followed by its final StepOutput. AgentOS can then expose those events through the workflow stream.
Use .arun() for an agent or team called from an async function executor.
custom_function_step_async_stream.py
Streaming works the same way with a class-based executor. Define the __call__ method to yield the events.

Install the Streaming Dependencies

Set OpenAI Key

Set your OPENAI_API_KEY as an environment variable. You can get one from OpenAI.

Developer Resources