> For the complete documentation index, see [llms.txt](https://gdplabs.gitbook.io/sdk/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gdplabs.gitbook.io/sdk/gl-runner/tutorials/sandbox-custom-dependencies.md). Agents can also search and read this documentation through its [MCP server](https://gdplabs.gitbook.io/sdk/~gitbook/mcp).

# Sandbox Custom Dependencies

A minimal runnable that declares its own Python package — `qrcode` — in a bundle-root `requirements.txt` and renders an ASCII QR code with it inside the sandbox. `qrcode` is not part of the worker's `worker` + `worker-runtime` closure, so a successful run is proof the declaration was built into a **derived template** and provisioned from it, not the operator default.

Use this tutorial to verify that:

* A bundle-root `requirements.txt` is picked up, validated, and digested at deploy time
* Deploying it builds a derived template (E2B or OpenSandbox) layering the declared package on top of the operator default
* The runnable's sandboxed runs provision from that derived template — importing a package the operator default never installed

## Bundle layout

```
examples/bundles/sandbox-custom-deps/
  entrypoint.py
  requirements.txt   ← declares the package the operator default does not have
```

```
# requirements.txt
qrcode==8.2
```

See [Bundle Format — Declaring per-runnable dependencies](https://gdplabs.gitbook.io/sdk/gl-runner/resources/runtime-environment.md#declaring-per-runnable-dependencies) for the full declaration contract (accepted line forms, validation errors, and the worker-owned-package override warning).

## Entrypoint

```python
import qrcode

def main(payload: dict | None = None, context: dict | None = None) -> dict:
    text = (payload or {}).get("text") or "https://pypi.org/project/gl-runner-sdk/"
    qr = qrcode.QRCode(border=1)
    qr.add_data(text)
    qr.make(fit=True)
    ...
    return {"text": text, "qr_ascii": ..., "qrcode_version": ..., "python_version": ...}
```

`qrcode_version` and `python_version` come from inside the sandbox, so they reflect the derived template's environment, not your local machine.

## Deploy definition

The deployment definition lives in `13_sandbox_custom_dependencies.py`:

```python
runnable.deploy(
    key="sandbox-custom-deps",
    kind="code",
    entrypoint="entrypoint.py:main",
    metadata={
        "name": "sandbox-custom-deps",
        "description": "Renders an ASCII QR code using a per-runnable declared dependency (qrcode).",
    },
    config={
        "sandbox": {"enabled": True, "backend": "opensandbox", "timeout_seconds": 120},  # or: "e2b"
    },
    # The first build of a new declaration can run for the server's full budget; the SDK
    # default of 120s would give up long before that. See "Run the example script" below.
    activation_timeout=960.0,
    activation_poll_interval=10.0,
)
```

Deploying prints the `sandbox_dependencies` summary from the response:

```python
{"declared": 1, "declaration_digest": "...", "warnings": []}
```

An empty `warnings` list here means `sandbox.enabled` was `true` and the backend supports building (`opensandbox` and `e2b` both do) — the declaration is being built into a template, not merely recorded. See [Declaring per-runnable dependencies](https://gdplabs.gitbook.io/sdk/gl-runner/resources/runtime-environment.md#declaring-per-runnable-dependencies) for what a non-empty `warnings` entry means (sandboxing disabled, or a declared package overriding a worker-pinned one).

`sandbox.enabled` and `backend` must both be right **in this deploy call**. Enabling sandboxing later through a config update, or switching between `opensandbox` and `e2b` afterwards, does not build a template for the already-deployed version — it fails the run instead of falling back to the operator default. Redeploy to change either one.

## Run the example script

From `python/gl-runner-sdk/`:

```bash
# Copy and fill examples/.env first (GL_RUNNER_BASE_URL, GL_RUNNER_API_KEY)
uv run python examples/13_sandbox_custom_dependencies.py
```

The first deploy of a new declaration builds the derived template, so it takes noticeably longer than a plain deploy — up to `GL_RUNNER_SANDBOX_TEMPLATE_BUILD_TIMEOUT_SECONDS` (operator setting, default 900s) in the worst case. `deploy()`'s own `activation_timeout` defaults to 120s, far short of that, so the script passes `activation_timeout=960.0` (a buffer past the server's cutoff) and a coarser `activation_poll_interval=10.0` so it doesn't give up — and doesn't spam the status endpoint — while the build runs. A later redeploy of the **same** declaration against an unchanged operator default reuses the existing derived template and returns quickly, well within the SDK default.

## Expected output

```
 ▄▄▄▄▄▄▄ ▄▄ ▄▄▄▄▄▄ ▄   ▄▄▄▄▄▄▄ 
 █ ▄▄▄ █  ▄█▀ ▀  ███▀▄ █ ▄▄▄ █ 
 █ ███ █ ██▄█▀▀██▄ ▀   █ ███ █ 
 █▄▄▄▄▄█ █▀█ █ █ █ ▄▀█ █▄▄▄▄▄█ 
 ▄   ▄ ▄▄█▀▄█▄ ██ █▄▀▄▄▄▄▄▄  ▄ 
 ▄▀▀▄▄ ▄ ▄███▀▄██▄▀█ ▄ ▀ ▀▀▀▀█ 
 ▀ ▄▄▀▄▄  ▄▀ ▀▄█ ██▀▀██ ▀▀▀ █▀ 
   ▀▀▀▀▄ █▄▄▀█▄██▀ █   ▀█▀██▀█ 
  ▄█ ▄▄▄▀▀███ █ ▀ █▀█ ▄ █   █▀ 
 ▀▀▄▄▄▀▄ ▀  ▀▄▀▀████  ██▀▀▀▄▀█ 
 ▄▄ ▄█▄▄ █ ▄  ▄█▄ █▄▄▄█▄██▄ ▀█ 
 ▄▄▄▄▄▄▄ ██▀▀█▄▄████▄█ ▄ █▄▄ █ 
 █ ▄▄▄ █ ▄▀█▀▄█ ▄ █▀██▄▄▄█▄ ▄  
 █ ███ █  ▀▄█ ▄██▀  ▄▄█ ▀ ▄▄▄█ 
 █▄▄▄▄▄█ ▄▀▀▀ ▀▄▀▄█ ▄█▄█▀▄▀ █▀ 
{'text': 'https://pypi.org/project/gl-runner-sdk/', 'qrcode_version': '8.2', 'python_version': '3.12.x'}
```

The block above is literal `qrcode==8.2` output for the default payload; encoding different text yields different art. `qrcode_version` confirms the declared package (not any worker-owned package) was resolved, and `python_version` reflects the sandbox's interpreter.

## What can go wrong

A bad declaration fails the **deploy**, not a later run. If pip cannot resolve the requirements against the worker's frozen closure, or the backend build itself errors, the new runnable version lands `failed` and never activates — so the declaration is never carried into a run. The backend's own output is kept verbatim on the version's `deploy_error`, prefixed `sandbox_template_build:`:

```python
version = runnable.versions(limit=1)[0]  # newest first
print(version.status)        # "failed"
print(version.deploy_error)  # "sandbox_template_build: ..."
```

How that surfaces to `deploy()` depends on whether the runnable had an active version already:

* **First deploy** — nothing survives to serve, the runnable goes `inactive`, and `deploy()` raises `DeployFailedError("Deploy failed: runnable ... became inactive.")`, a `RuntimeError`.
* **Redeploy over a working version** — the previous version keeps serving, so `deploy()` returns normally and runs keep working on the **old** code. Check the newest version's `status` after a deploy that changed `requirements.txt`; a green `deploy()` is not proof the new version shipped.

A `TimeoutError` from `deploy()` is a different thing and is **not** a failure: the build keeps running server-side and the version activates when it finishes. Check `status` before redeploying — a blind retry queues a second build.

## Operator prerequisites

Same as [Sandbox Weather](https://gdplabs.gitbook.io/sdk/gl-runner/tutorials/sandbox-weather.md#operator-prerequisites), plus one extra requirement this tutorial actually exercises: the **API server** — not just the worker — needs backend credentials and egress, because deploying a declaration builds its derived template in-process on the server. Concretely, the server needs:

* **E2B**: `GL_RUNNER_SANDBOX_E2B_API_KEY` (plus `GL_RUNNER_SANDBOX_E2B_DOMAIN` for self-hosted E2B) and `GL_RUNNER_SANDBOX_E2B_TEMPLATE`, which names the base alias the derived template chains from.
* **OpenSandbox**: the `GL_RUNNER_SANDBOX_OPENSANDBOX_*` connection settings (`DOMAIN`, `PROTOCOL`, `API_KEY`, `USE_SERVER_PROXY`) and `GL_RUNNER_SANDBOX_OPENSANDBOX_SNAPSHOT_NAME`, which names the base the derived snapshot chains from.
* **Network egress from the API server to the sandbox control plane** — a new direction of traffic, since previously only workers reached it.
* `GL_RUNNER_SANDBOX_TEMPLATE_BUILD_TIMEOUT_SECONDS` (default 900) bounds one build.

Without these, deploys of bundles that *declare* dependencies fail with a backend authentication or connection error; deploys of bundles that declare nothing are unaffected. Ask your gl-runner operator to confirm the server side is configured before running this tutorial.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://gdplabs.gitbook.io/sdk/gl-runner/tutorials/sandbox-custom-dependencies.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
