Skip to main content Switch to mobile version lexq 0.1.0 The Python client for the LexQ execution API.
pip install lexq Copy PIP instructions
The Python client for the LexQ execution API. It needs Python 3.11 or later and nothing outside Python's standard library.
python -m pip install lexq The Execution API reference describes the API this package calls, and When LexQ Is Slow or Unavailable covers what to do when a call fails.
Your backend sends facts, such as an order's amount, and LexQ evaluates your rules and returns the decision. This package makes that call, with four safeguards:
Every call has a deadline that covers its retries.
An attempt that times out or hits a temporary error is sent again with the same Idempotency-Key. If LexQ has already finished an earlier attempt successfully, it returns that result instead of running the decision again, unless a fault on LexQ's side keeps it from finding that result. LexQ may have run an attempt that timed out, lost its connection, or got a 502 or 504, so these failures share one retry per call. An attempt that could not connect, or got back something other than an HTTP answer the client can read, such as a 101, counts as one of them. After a 503, a 429, or I-002, the call tries again while its deadline and the limit of 4 attempts allow. In an organization that uses Fail-open, LexQ keeps running calls through some outages in its own infrastructure. If the first attempt or a retry arrives during an outage that keeps LexQ from checking whether the key is already in use, a retry that arrives before LexQ has finished an earlier attempt may run the decision again and be billed as a new call.
A circuit breaker stops calling LexQ for a while when calls keep failing.
Every call requires a fallback, which returns the value your service uses when the call fails.
spec/SPEC.md spells out each safeguard and how the package reads LexQ's responses. It is an unchanged copy of one version of the LexQ SDK specification. CONTRACT.md says which version that is, where the package does not yet do what that version says, and how the specification maps onto Python.
Run the client on a server. It refuses to start in a web page that runs Python through Pyodide, and in iOS, tvOS, visionOS, watchOS, and Android apps built with Python 3.13 or later, where the API key would reach every user of the page or the app. Keep it out of a desktop app, or any other app that runs on its users' own machines, and out of a mobile app built with Python 3.11 or 3.12, for the same reason. The client can start there, because it cannot always tell such an app from a server. The item "Where the client starts" in CONTRACT.md says more.
import os from decimal import Decimal import lexq # Create one client and share it: its circuit breaker and connections belong to it. client = lexq.LexqClient(os.environ["LEXQ_API_KEY"]) amount = Decimal("150000") result = client.execute( group_id, {"paymentAmount": amount, "customerTier": "VIP"}, # A/B routing reads trafficKey from the contexts. In the facts it is ignored. # Make it a surrogate key or a keyed hash, never an email address or a phone number. contexts={"trafficKey": customer_key}, idempotency_key=f"checkout-{order_id}", # What your service does when the call fails: here, charge the full amount. fallback=lambda error: amount, ) if result.source == "lexq": # Store result.trace_id with the order. # A rule that changed paymentAmount put the new value in mutated_facts. charge = result.mutated_facts.get("paymentAmount", amount) # A BLOCK action does not fail the call. It sets isBlocked: true in one of the layers. blocked = any( layer.get("isBlocked") is True for layer in (result.generated_variables, result.mutated_facts, result.input_facts) ) # result.decision_traces says why each rule LexQ evaluated fired or did not. else: # result.value is what the fallback returned, and result.error says why. charge = result.value A DecisionResult carries LexQ's data as it came back. data holds every field, including any that LexQ adds later, and each field that the Execution API reference shows in data for single-group execution is also a property, in snake case: trace_id, executed_at, total_processing_time_ms, input_facts, input_contexts, mutated_facts, generated_variables, execution_traces, and decision_traces. The facts come in three layers: input_facts as LexQ received them, mutated_facts with the new value of each fact the request sent that a rule changed, and generated_variables with the keys the request did not send, such as a SET_FACT target. A MUTATE_FACT that changes paymentAmount also adds paymentAmount__delta to generated_variables: the net change from the value the request sent, not the new value. The property for a layer, the contexts, or a trace list that LexQ leaves out or sends as null is empty, so you can read it without checking for None. data cannot be changed, but the dicts and lists in it can.
To send a decision's facts in a later call, take each value from mutated_facts or generated_variables when it is there and from input_facts otherwise, and leave out isBlocked and the keys that end in __delta. LexQ answers 400 (P-068) when a key that ends in __delta is directly under facts, and the fallback runs. Send an earlier block under another key, such as previouslyBlocked: a request that sends isBlocked: true looks blocked whether or not a rule blocked it.
Every number in a decision's facts, contexts, and traces is a Decimal with the digits and the exponent LexQ wrote, so 1.50 stays Decimal('1.50'). total_processing_time_ms is an int. Compare two numbers with ==, which compares by value: Decimal('20') == Decimal('20.0') is True. To send an exact amount, pass a Decimal. A float goes out as repr() writes it, so 0.1 goes out as 0.1 and 0.0000001 as 1e-07. CONTRACT.md gives the text for every number type and lists the values the client refuses to send. A string such as "150000" stays a string, and when an enabled rule reads a fact registered as NUMBER, LexQ can answer it with 400 (P-060).
To send a fact you have no value for, put None in its place. LexQ keeps the key, but a condition on a null value does not match, even in a rule that blocks. Make sure such a request still gets a decision, as When an input is null shows. If an enabled rule reads a key that the facts leave out, LexQ answers 400 (P-015) and the call uses the fallback. Required vs Optional Facts says which keys a request must send, and what happens when a MUTATE_FACT target is null.
LexQ stores the whole contexts object and the idempotency key with the execution, so keep personal data out of both. An idempotency key is one or more printable ASCII characters with no space at either end, and LexQ takes up to 255 of them, so hash an ID that is longer or not ASCII.
By default a call has a 15-second deadline and 10 seconds per attempt. Pass a shorter deadline, in seconds, to execute for a request that cannot wait that long. The deadline covers every attempt and every wait between them, but not the fallback. When the deadline runs out after a request was sent, LexQ may still finish and bill that decision. If it succeeded, repeating the call within 24 hours with the error's idempotency_key returns it instead of running it again, unless a fault on LexQ's side keeps LexQ from finding it. Retrying a Failed Call says what a repeat gets otherwise.
A KeyboardInterrupt or SystemExit raised while execute waits passes through: the client closes the attempt in flight, and the fallback does not run. Such a call does not report its idempotency key, so pass your own with idempotency_key when a call may be interrupted but you still want its decision.
A call that makes an attempt counts toward the circuit breaker when its deadline runs out, or its last attempt times out, cannot connect, loses its connection, gets back something other than an HTTP answer the client can read, such as a 101 that switches protocols, or gets a 5xx that does not carry P-003. P-003 means an action of one of your rules failed, not that LexQ is down. By default, after 5 counted calls in a row, calls go straight to the fallback for 10 seconds, and then one trial call goes through while the others still get the fallback. If the trial counts, the breaker opens again. If the trial ends with an answer that does not count, the breaker closes. If the trial makes no attempt or is interrupted, the next call is the trial.
The fallback receives a LexqError. reason says why the call ended without LexQ's decision, and status and error_code hold the HTTP status and errorCode of the answer that ended the call, if an answer did. When that answer reports a failed execution that LexQ recorded, such as one that got P-015, trace_id holds the trace ID of its record: store it as you would a decision's trace_id. A 500 with C-004 carries no trace ID. attempts lists every attempt, and idempotency_key holds the key the call used, unless the key itself was refused. The message is one line and can include LexQ's own message, but never the API key or a raw response body. When the client refuses a value in the facts or the contexts, the message names it by its key path, such as facts.items[3], instead of quoting it. The package logs nothing itself.
execute waits, and runs the fallback in the calling thread. One client is safe to call from many threads at once, and those calls share its circuit breaker and its connections. The client keeps a connection open after an answer and sends a later call on it, which saves the time a new connection takes. It sends nothing on a connection that has gone unused for 30 seconds, or for half the time that the answer's Keep-Alive header gives when that is shorter. A server that starts its workers by forking can create the client before it forks: a worker never sends on a connection that another process kept. The signatures of LexqClient and execute list every option and its default.
To check that the copies in spec/ match the SHA-256 values in spec/spec.lock, and that CONTRACT.md names the version the lock records, run python scripts/check_spec.py from the repository root with Python 3.11 or later.
To build the package and run its tests, run these from the repository root in a virtual environment with Python 3.11 or later:
python -m pip install --no-deps -r requirements/build.txt python -m build --no-isolation python -m pip install --no-deps --no-index --find-links dist --force-reinstall lexq python -m unittest discover -s tests The tests import the installed package, so build and install it again after a change to src/. The HTTPS tests also need the openssl command: each time they run, they make a test certificate authority and a certificate for localhost with it, so that no key is kept in the repository. To check that mypy and pyright report a call without a fallback, run python -m pip install --no-deps -r requirements/typecheck.txt and then python scripts/check_types.py in the same environment. To write src/lexq/_contract.py again from spec/values.json and the version in pyproject.toml, run python scripts/generate_contract.py from the repository root.
I'm Sanghyun Park, and I maintain this package. I write most of it with AI coding tools, mainly Claude Code. I decide each change, read every commit and diff before I push it, and merge every pull request myself. Dependabot pushes its own branches to propose new versions of the CI actions and of the packages pinned under requirements/, and I read those changes before I merge them. The rules for changing the package are in CONTRIBUTING.md.
Report a vulnerability privately, as described in SECURITY.md.
Released: Dec 4, 2026
Both files in this release have provenance metadata.
View provenance details
Apache-2.0 View SPDX License List
Release files for lexq 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
Built distribution (wheel)
Total release size: 83.3 kB
Release files / lexq-0.1.0.tar.gz
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 4, 2026.
Release files / lexq-0.1.0-py3-none-any.whl
Security for lexq 0.1.0
Both files in this release have been uploaded with provenance metadata.
With provenance lexq-0.1.0.tar.gz Dec 4, 2026
With provenance lexq-0.1.0-py3-none-any.whl Dec 4, 2026
Attestation for lexq-0.1.0.tar.gz
Attestation for lexq-0.1.0-py3-none-any.whl
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.
Release history Release notifications | RSS feed
Dec 4, 2026 2 release files Full provenance
"PyPI", "Python Package Index", and the blocks logos are registered trademarks of the Python Software Foundation.
© 2026 Python Software Foundation
Deployed from 1274d9b











