Skip to content

Sequential API

Open In Colab

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="ollama/mistral:latest")

    # 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",
    )

Source

--8 < --"examples/1c_sequential.py:109"

Run log

The log below is the unedited combined output of running the example above with local models (ollama).

Full run log: examples/1c_sequential.log
============================================================
Building a Chain-of-Thought Program (Sequential API)
============================================================

Running the program...
------------------------------------------------------------

Result:
{
  "thinking": "The key aspects of human cognition include perception, attention, memory, learning, reasoning, problem-solving, decision making, and language.",
  "answer": "Perception, attention, memory, learning, reasoning, problem-solving, decision making, and language are the key aspects of human cognition."
}

============================================================
Accessing individual fields:
============================================================

Thinking: The key aspects of human cognition include perception, attention, memory, learning, reasoning, probl...

Answer: Perception, attention, memory, learning, reasoning, problem-solving, decision making, and language a...