Skip to content

Sequential API

The Sequential API

You've learned two ways to build programs: the Functional API (Lesson 1a) and Subclassing (Lesson 1b). Now, let's explore the simplest approach: the Sequential API.

When to Use Sequential

The Sequential API is perfect when your program is a simple pipeline - data flows through modules one after another, like water through pipes:

graph LR
    Input --> A[Module A] --> B[Module B] --> C[Module C] --> Output

No branching, no conditionals, just a straight line of transformations.

Comparison: Three Ways to Build Programs

API Use Case Complexity
Sequential Simple linear pipelines Easiest
Functional Graphs with branches/merges Medium
Subclassing Custom logic, full control Advanced

The Sequential Pattern

Building with Sequential is as simple as making a list:

program = synalinks.Sequential(
    [
        synalinks.Input(data_model=InputType),     # First: where data enters
        synalinks.SomeModule(...),                  # Middle: transformations
        synalinks.Generator(data_model=OutputType), # Last: final output
    ],
    name="my_program",
)

Think of it like a recipe:

  1. Start with ingredients (Input)
  2. Apply steps in order (Modules)
  3. Get the final dish (Output)

Complete Example

import asyncio
from dotenv import load_dotenv
import synalinks

class Query(synalinks.DataModel):
    query: str = synalinks.Field(description="The user query")

class AnswerWithThinking(synalinks.DataModel):
    thinking: str = synalinks.Field(description="Your step by step thinking")
    answer: str = synalinks.Field(description="The correct answer")

async def main():
    load_dotenv()
    language_model = synalinks.LanguageModel(model="openai/gpt-4.1")

    # Create a sequential program - just a list of modules!
    program = synalinks.Sequential(
        [
            synalinks.Input(data_model=Query),
            synalinks.Generator(
                data_model=AnswerWithThinking,
                language_model=language_model,
            ),
        ],
        name="chain_of_thought",
    )

    result = await program(Query(query="What is the capital of France?"))
    print(f"Answer: {result['answer']}")

asyncio.run(main())

Key Takeaways

  • Sequential API: The simplest way to build programs - just provide a list of modules that execute in order.
  • Linear Pipelines: Best for simple data flows with no branching or conditional logic.
  • Minimal Boilerplate: No need to manually connect inputs/outputs - Sequential handles the wiring automatically.
  • Quick Prototyping: Great for testing ideas quickly before moving to more complex architectures.

Program Visualization

chain_of_thought

API References

AnswerWithThinking

Bases: DataModel

The output from our program - reasoning + final answer.

Source code in examples/1c_sequential.py
class AnswerWithThinking(synalinks.DataModel):
    """The output from our program - reasoning + final answer."""

    thinking: str = synalinks.Field(
        description="Your step by step thinking",
    )
    answer: str = synalinks.Field(
        description="The correct answer",
    )

Query

Bases: DataModel

The input to our program - a user's question.

Source code in examples/1c_sequential.py
class Query(synalinks.DataModel):
    """The input to our program - a user's question."""

    query: str = synalinks.Field(
        description="The user query",
    )