NOETIC
Framework
Operators

conditional

Conditional routing -- pick one step to execute or skip entirely.

Quick Example

import { callModel, conditional } from '@noetic-tools/core';

const triageAgent = conditional({
  id: 'triage',
  route: (input: string, ctx) => {
    if (input.includes('urgent')) {
      return callModel({
        id: 'urgent-handler',
        model: 'openai/gpt-4o',
        instructions: 'Handle this urgent request immediately.',
      });
    }
    if (input.includes('ignore')) {
      return null; // skip -- no step executed
    }
    return callModel({
      id: 'default-handler',
      model: 'openai/gpt-4o-mini',
      instructions: 'Handle this routine request.',
    });
  },
});

What It Does

conditional is a runtime router. Its route function receives the step input and context, and returns either:

  • A Step to execute, or
  • null to skip (the input passes through as-is).

This is the idiomatic way to add conditional logic to an Noetic pipeline. Instead of if/else blocks around step calls, you express the decision as data.

API Reference

PropertyTypeRequiredDescription
idstringYesUnique step identifier
route(input: I, ctx: Context<TContext>) => Step<TContext, I, O> | null | Promise<Step<TContext, I, O> | null>YesFunction that picks the step to run (sync or async)

How Null Routes Work

When route returns null, the conditional is skipped entirely. The input value is passed through as the output. This is useful for optional processing steps.

import { callModel, conditional } from '@noetic-tools/core';

const maybeTranslate = conditional({
  id: 'maybe-translate',
  route: (input: { text: string; lang: string }) => {
    if (input.lang === 'en') {
      return null; // already English, skip
    }
    return callModel({
      id: 'translate',
      model: 'openai/gpt-4o',
      instructions: `Translate the following text to English.`,
    });
  },
});

Dynamic Step Creation

The route function creates steps at runtime, so you can parameterize them based on input.

import { callModel, conditional } from '@noetic-tools/core';

const modelPicker = conditional({
  id: 'model-picker',
  route: (input: { task: string; budget: 'low' | 'high' }) => {
    const model = input.budget === 'high' ? 'openai/gpt-4o' : 'openai/gpt-4o-mini';
    return callModel({
      id: `${input.task}-llm`,
      model,
      instructions: `Perform the following task: ${input.task}`,
    });
  },
});

Nesting Conditionals

A conditional's route can return other conditionals, inParallel groups, or any other step type.

import { callModel, conditional, inParallel } from '@noetic-tools/core';

const nested = conditional({
  id: 'outer',
  route: (input: string) => {
    if (input === 'compare') {
      return inParallel({
        id: 'compare-models',
        mode: 'all',
        paths: () => [
          callModel({
            id: 'model-a',
            model: 'openai/gpt-4o',
          }),
          callModel({
            id: 'model-b',
            model: 'openai/gpt-4o-mini',
          }),
        ],
        merge: (results) => results.join('\n---\n'),
      });
    }
    return callModel({
      id: 'single',
      model: 'openai/gpt-4o',
    });
  },
});

Semantic Routing

The route function can be async, enabling AI-powered and embedding-based routing. Noetic provides condition helpers like semanticSwitch, semanticRoute, embeddingMatch, and aiCondition that return async route functions:

import { callModel, conditional, semanticSwitch } from '@noetic-tools/core';

const router = conditional({
  id: 'semantic-router',
  route: semanticSwitch({
    embed,
    cases: {
      'greeting or salutation': callModel({ id: 'greet', model: 'openai/gpt-4o-mini' }),
      'technical question': callModel({ id: 'tech', model: 'openai/gpt-4o' }),
    },
    threshold: 0.7,
  }),
});

See Semantic Conditions for the full API, including condition combinators, caching, and custom conditions.

  • inParallel -- parallel execution for when you need multiple paths at once.
  • Steps -- the step types you can return from a route function.
  • Loop & Until -- combine conditional routing with iteration.

On this page