Skip to main content
Train a language model to write correct Python functions using reinforcement learning — without ever running untrusted model-generated code in your training process. This guide walks through a two-phase setup: a supervised fine-tuning warmup followed by GSPO fine-tuning, where every completion is evaluated inside an isolated TensorLake sandbox running a hidden pytest suite.

How it works

  1. SFT warmup (Phase 1): Supervised pass on correct solutions so the model starts generating valid Python. Without this, all completions score 0, reward variance is 0, and the RL trainer has no gradient signal.
  2. GSPO fine-tuning (Phase 2): The GRPOTrainer (with importance_sampling_level="sequence") generates G completions per step and dispatches them to G parallel sandboxes.
  3. Sandbox reward: Each sandbox runs a hidden pytest suite against the model’s code and returns tests_passed / total as the reward signal (0.0–1.0).
  4. Why sandboxes are required: Model-generated code is untrusted. Running it in-process during training would be unsafe. Each completion is fully isolated.

GSPO vs GRPO

Both algorithms use clipped importance sampling, but at different granularities: For long function bodies, token-level clipping lets noisy individual tokens dominate the gradient. Sequence-level clipping treats the entire trajectory as one unit, which is a better fit for code generation tasks.

Prerequisites

Create a .env file in your project root with your Tensorlake API key:

TypeScript SDK starter

In Node.js, the critical part is still the reward oracle: each completion gets written into its own sandbox, the hidden pytest suite runs there, and the pass ratio becomes the reward.
That reward function plugs into the same GSPO loop described below. The model/trainer side can stay in Python, but the sandbox evaluation path can be moved to TypeScript if your orchestration layer already lives there.

Full example

The script below runs end-to-end: baseline evaluation → SFT warmup → GSPO fine-tuning → final evaluation. Pass --smoke for a fast 5-minute CPU run (3 tasks, 20 SFT steps, 1 GSPO epoch).

What happens step-by-step


Key design decisions

Why temperature=1.4

GSPO requires diversity across the G completions in each group to produce a non-zero reward standard deviation. If all completions are identical (low temperature), reward_std = 0 and the advantage normalization produces zero gradients — training stalls. Setting temperature high forces the model to explore different implementations.

Why SFT warmup is required

Without warmup, a randomly-initialized or instruction-tuned model produces malformed Python that scores 0 on every test case. All-zero rewards mean all-zero advantages after normalization, and GSPO has nothing to optimize. Even 20 supervised steps on correct solutions is enough to bootstrap non-zero reward variance.

Why sandboxes prevent reward hacking

The model never has access to the test file. The only feedback is the pass rate returned by the sandbox. This makes it impossible for the model to overfit to specific assertion patterns — it must actually implement the correct logic.
This example uses python-dotenv to load your Tensorlake API key. Create a .env file in your project root:
The SDK will pick it up automatically.

What to build next

AI Code Execution

Use a sandbox as a tool inside an agentic LLM loop.

Agentic Swarm Intelligence

Dispatch parallel sandboxes across a swarm of worker agents.