Functions and Scaling Groups
Define and call remote functions that run on Chalk.
A Chalk Function is a Python callable that you deploy onto a
Scaling Group and register in the
Chalk Catalog. Decorate a function
with @chalkcompute.function and it becomes reachable from anywhere Chalk
runs: local scripts and notebooks, other functions, and (because of the
Catalog registration) the feature engine, including Chalk SQL, resolvers, the
query server, and Chalk DataFrames (chalkdf) via F.catalog_call(). Arguments and results are
serialized automatically; you write ordinary Python and Chalk handles
scheduling, scaling, and delivery.
On top of what a bare Scaling Group provides, functions add async / streaming invocation, nested calls, automatic retries, concurrency caps, rate limits, and request-queue-driven autoscaling. Reach for a function when you want any of those features, or when you want the callable to be visible to the rest of Chalk.
Functions support three return styles:
yield results incrementally and the caller receives them as
they’re produced. See Streaming results
for an end-to-end example.To define and deploy a function, see Defining a function.
For jobs with multiple steps and separate task environments, see Workflows.
Call a deployed function from Python with the chalkcompute SDK, using .remote() or
.defer(), or from inside Chalk through the
Chalk Catalog. Functions have no HTTP or REST
route, and the Python SDK is the only published client for calling them.
Call a deployed function on each row with functions.<name>(...). For example,
if you deploy an extract_entities function that finds people and organizations
in text, call it on each transaction’s memo:
SELECT
transaction_id,
functions.extract_entities(memo) AS entities
FROM
transactionsfunctions.extract_entities(memo) resolves the same deployed function as
catalog_call('function.extract_entities', memo).
See Call Chalk Functions from SQL for a deployable example and how Chalk runs eligible Python functions inside the SQL query plan.
You can pass the function’s declared inputs by name, in any order. Positional
arguments must come before named arguments. For a function declared as
fetch_url(url, timeout), both url and timeout belong to that function:
SELECT
functions.fetch_url(timeout => 5, url => release_url) AS response
FROM
releasesNamed calls require the deployed function signature to be available in the catalog. Supply all declared inputs; SQL does not fill in Python default values. Argument names must match the declared parameter names, including case.
Set Chalk’s invocation options on with_policies(...). These include
asynchronous, timeout, on_error, metadata, and row_metadata, alongside
policies such as rate_limit and retry:
SELECT
with_policies(
functions.fetch_url(url => release_url, timeout => 5),
asynchronous => TRUE,
timeout => 30,
on_error => 'struct',
concurrency => {'max_concurrent':25},
rate_limit => {'rate':2}
) AS response
FROM
releasesThe SQL rate_limit defaults to calls per second. If you omit key from
rate_limit or concurrency, each call site has its own limit for the duration
of the query execution. Rerunning the query starts with a fresh limiter. Set
the same key on multiple call sites to share a limit of the same policy type.
Set an explicit key when using enforce_globally.
To see when a call started and ended, set on_error => 'struct'. Each row gets
a struct with value, error, started_at, and ended_at. Both successful
and failed calls include the timestamps. On success, error is NULL. If a
request fails, the query continues and each affected row has a NULL value and
error details in error. With the default on_error => 'fail', a successful
call returns the function value and a failed request fails the query.
The inner timeout is a function argument. The outer timeout bounds each
Chalk request. asynchronous => true routes the invocation through the function
queue; the query waits for the result. It must be a non-null constant boolean
and defaults to false. You can use with_policies with invocation options
alone, without adding a rate limit or retry policy. Nested wrappers preserve
existing options and policies, and outer values override the options they supply.
Use a quoted identifier for function names containing punctuation, such as
functions."extract-entities"(memo). SQL normalizes function names to lowercase,
including quoted names. To call a catalog entry with uppercase letters in its
name, use catalog_call('function.ExtractEntities', memo).
Existing catalog_call(...) and catalog_call_async(...) calls retain their
named invocation options. catalog_call(...) also accepts asynchronous => true.
The catalog_call_async(...) spelling selects the queue and rejects
asynchronous => false inside that call.
Their named options configure Chalk; use functions.<name>(...) for the function’s
own named arguments.
To call code that runs on Chalk from TypeScript or another language, use one of these routes:
authenticated=True, and send a Chalk access
token as a bearer token on each request. A service can get a token from
POST /v1/oauth/token.A synchronous .remote() call goes through the environment’s gateway to a worker and returns
the result. A .defer() call, or a .remote() call to a generator function, goes through three
moving parts:
┌──────────┐ ┌──────────────────┐ ┌───────────┐
│ Caller │──call──▸│ Function Queue │──run───▸│ Worker │
│ │◂results─│ │◂results─│ (Python) │
└──────────┘ └──────────────────┘ └───────────┘Callers and workers are loosely coupled: the caller never opens a connection directly to a worker. Workers can scale up, scale down, or get rescheduled mid-flight and in-flight calls are not lost. This is what enables streaming async generators, nested calls, and policy-enforced dispatch.
The queue enforces the policies declared on each function (rate limit, concurrency, and retries) before dispatching work to the worker.
| Policy | What it caps |
|---|---|
rate_limit | Outbound requests per second / minute / hour, optionally shared across functions via a key. |
concurrency | In-flight invocations at once, optionally shared across functions via a key. Defaults to 1 per function. |
retries | Retries of a failed call, after a fixed delay. Any exception the function raises is retried until the count runs out. Applies to .defer(...) calls and to .remote(...) calls to generator functions. |
Concurrency is acquired before dispatching, so a burst of queued calls backpressures on the semaphore rather than spawning unbounded tasks. Rate-limit tokens are consumed per attempt: each retry takes a fresh token.
When a generator function has retries set, the queue holds each attempt’s output until that attempt succeeds or is the last one. The caller does not receive values from an attempt that is then retried, so a retry does not repeat values the caller already has.