Skip to content

feat(gfql): restore lambda support in filter_dict for local-only flows #967

Description

@lmeyerov

Summary

Lambda functions in filter_dict (e.g., n({"score": lambda x: (x > 50) & (x % 2 == 0)})) were removed when AST validation was tightened to require JSON-serializable values for the wire protocol and remote execution.

This is a regression for local-only workflows where serialization isn't needed. Lambdas were a convenient way to express compound conditions that don't map cleanly to a single predicate.

RCA

The check at graphistry/compute/ast.py:167 rejects any filter_dict value that isn't an ASTPredicate or JSON-serializable. Lambdas/callables fail this check.

The reason is wire protocol: filter_dict values need to round-trip through JSON for to_json() / from_json() and remote execution via gfql_remote().

Proposed

For local-only execution (g.gfql([...]), not g.gfql_remote([...])), allow callables in filter_dict:

  • Accept callable values at AST construction time (skip the JSON-serializable check)
  • In filter_by_dict(), apply callables directly: df[df[col].apply(val)]
  • Raise at serialization time (to_json()) if a callable is present, not at construction time

This preserves the wire protocol constraint while restoring local usability.

Current workaround

# Instead of: n({"score": lambda x: (x > 50) & (x % 2 == 0)})
# Use query string:
n(query="score > 50 and score % 2 == 0")

Activity

  1. lmeyerov commented on Mar 30, 2026

    @lmeyerov
    ContributorAuthor

    Design note: numba UDFs vs lambda restoration

    Investigated whether numba-jitted functions could provide a cuDF-compatible lambda path:

    • cuDF Series.apply() requires numba-jitted functions (not plain Python lambdas)
    • numba UDFs are restricted to numeric operations — no string filtering, no complex logic
    • numba is a heavy dependency for marginal benefit

    Recommendation: Don't add numba. Instead:

    1. Restore plain lambdas for local-only flows — defer the JSON-serializable check from AST construction (ast.py:167) to to_json() time. Lambdas work fine on pandas Series.apply(). Raise at serialization time if someone tries to_json() or gfql_remote() with a lambda in filter_dict.

    2. Keep query= strings as the primary cross-backend approach — df.query("score > 50 and score % 2 == 0") works on both pandas and cuDF with no serialization issues.

    This gives local users their lambdas back while keeping the wire protocol clean.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions