Skip to content

Conversation

@Smirl
Copy link

@Smirl Smirl commented Nov 18, 2025

Description: (required)

Fixes #1438

This pull request adds support for a new $length operator in the ConditionalRouter service, allowing conditional routing logic to evaluate the length of arrays. This enhancement makes it possible to write more expressive and flexible conditions based on array length, including direct comparisons or using other operators like $gt or $lt on the length.

New Operator Support:

  • Added Length ($length) to the Operator enum in conditionalRouter.ts to enable length-based conditions.

Conditional Evaluation Logic:

  • Implemented logic in ConditionalRouter to handle the $length operator, supporting both direct numeric comparisons and nested operator objects (e.g., { $gt: 5 }) for array lengths.

Tests Run/Test cases added: (required)

  • Tests for all conditional operators

Type of Change:

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes)

Smirl and others added 2 commits November 18, 2025 10:02
- Test basic equality matching with $length
- Test nested operators ($gt, $lt, $gte, $lte, $eq, $ne)
- Test edge cases (empty arrays, non-arrays, metadata arrays)
- Test complex conditional queries with $and
- Add regression tests for existing operators
- All 16 tests passing

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@narengogi
Copy link
Collaborator

@Smirl could you add a few examples on how you're using this new parameter in config?

@Smirl
Copy link
Author

Smirl commented Nov 19, 2025

I was trying to create a workaround whilst waiting for #1429. Then I noticed there wasn't a length or size operator so I added it. It won't help me on this occasion but being able to look at array types might be useful.

{
  "strategy": {
    "mode": "conditional",
    "conditions": [
      {
        "query": {
          "params.tools": {
            "$length": 0
          }
        },
        "then": "default-tool-router"
      }
    ],
    "default": "model-router"
  },
  "targets": [
    {
      "name": "model-router",
      "retry": {
        "attempts": 3
      },
      "cache": {
        "mode": "simple"
      },
      "override_params": {
        "model": "@bedrock-sandbox/eu.anthropic.claude-sonnet-4-5-20250929-v1:0"
      }
    },
    {
      "name": "default-tool-router",
      "retry": {
        "attempts": 3
      },
      "cache": {
        "mode": "simple"
      },
      "override_params": {
        "model": "@bedrock-sandbox/eu.anthropic.claude-sonnet-4-5-20250929-v1:0",
        "tools": [
          {
            "type": "function",
            "function": {
              "name": "noop",
              "description": "never call this tool",
              "input_schema": {
                "type": "object"
              }
            }
          }
        ]
      }
    }
  ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Add length conditional operator

2 participants