{"id":5590,"library":"cel-python","title":"Pure Python Common Expression Language (CEL)","description":"cel-python is a pure Python implementation of Google's Common Expression Language (CEL), currently at version 0.5.0. It provides common semantics for expression evaluation, enabling applications to interoperate through a fast, embeddable expression language. The library aims for minimal dependencies and is primarily used for security policies, such as within Cloud Custodian. Its release cadence is tied to its primary consumers.","status":"active","version":"0.5.0","language":"en","source_language":"en","source_url":"https://github.com/cloud-custodian/cel-python","tags":["expression-language","cel","security","policy","evaluation"],"install":[{"cmd":"pip install cel-python","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Used for regular expression evaluation as specified by CEL.","package":"re2","optional":false}],"imports":[{"note":"While Environment is in evaluation, it's exposed directly via celpy top-level for convenience.","wrong":"from celpy.evaluation import Environment","symbol":"Environment","correct":"from celpy import Environment"},{"note":"celtypes is a module within the celpy package, not a top-level module.","wrong":"import celtypes","symbol":"celtypes","correct":"from celpy import celtypes"}],"quickstart":{"code":"from celpy import Environment, celtypes\n\ndecls = {\"name\": celtypes.StringType}\nenv = Environment(annotations=decls)\nast = env.compile(\"'Hello world! I\\'m \" + name + \".'\")\nout = env.program(ast).evaluate({\"name\": \"CEL\"})\nprint(out)\n# Expected: Hello world! I'm CEL.","lang":"python","description":"This example demonstrates compiling and evaluating a simple CEL expression with a string variable, using the Environment and celtypes classes."},"warnings":[{"fix":"Ensure explicit type conversion within CEL expressions (e.g., `double(1) + 2.5` instead of `1 + 2.5`) if mixed types are intended, or ensure consistent typing of inputs.","message":"CEL's type semantics differ from native Python, and type coercion is generally not performed implicitly. This can lead to unexpected `TypeError`s if not handled explicitly in expressions.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Be aware of the truncation behavior for division operations and test expressions thoroughly, especially with negative numbers.","message":"Division behavior (`/`) in CEL truncates towards zero, which is different from Python's integer division (`//`) that truncates towards negative infinity. This can lead to subtle differences in arithmetic results.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Implement proper try-except blocks to catch `celpy.evaluation.EvalError` when evaluating expressions to handle potential runtime issues gracefully.","message":"CEL runtime errors are mapped to Python exceptions, specifically `celpy.evaluation.EvalError`. These errors provide a message similar to the CEL error, along with an underlying Python exception.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}