Skip to main content

Overview

You can develop and test OpenReward environments locally before deploying to production. This enables:
  • Rapid Iteration: Test changes instantly without waiting for deployments
  • Debugging: Use debuggers and inspect environment behavior
  • Offline Development: Work without internet connection
  • Cost Savings: No cloud resources used during development

Quick Start: Testing Environments Locally

1. Install Dependencies

pip install openreward

2. Write Your Environment Server

# server.py
from openreward.environments import Environment, Server, TextBlock, ToolOutput, tool
from pydantic import BaseModel

class AnswerParams(BaseModel):
    answer: str

class MyEnvironment(Environment):
    """A simple math environment"""

    @classmethod
    def list_tasks(cls, split: str):
        return [
            {"id": "task-1", "problem": "What is 2+2?", "answer": 4},
            {"id": "task-2", "problem": "What is 5*3?", "answer": 15},
        ]

    @classmethod
    def list_splits(cls):
        return ["train", "test"]

    def get_prompt(self):
        return [TextBlock(type="text", text=self.task_spec["problem"])]

    @tool
    def answer(self, params: AnswerParams) -> ToolOutput:
        """Submit your answer"""
        correct = str(self.task_spec["answer"]) == params.answer
        return ToolOutput(
            blocks=[TextBlock(type="text", text="Correct!" if correct else "Wrong!")],
            reward=1.0 if correct else 0.0,
            finished=True
        )

if __name__ == "__main__":
    Server([MyEnvironment]).run()

3. Run the Server

python server.py
Output:
INFO:     Started server process [12345]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:8080

4. Test the Environment

# test_environment.py
from openreward import OpenReward

or_client = OpenReward(base_url="http://localhost:8080") # you must point towards the environment if running locally

# Connect to local server (no namespace = localhost)
environment = or_client.environments.get(name="myenvironment")

# List tasks
tasks = environment.list_tasks(split="train")
print(f"Found {len(tasks)} tasks")

# Create session and test tool
with environment.session(task=tasks[0]) as session:
    prompt = session.get_prompt()
    print(f"Prompt: {prompt[0].text}")

    result = session.call_tool("answer", {"answer": "4"})
    print(f"Result: {result.blocks[0].text}")
    print(f"Reward: {result.reward}")
Alternatively, if you don’t want the whole client to point to http://localhost:8080 just change the base_url when getting the environment, like this:
environment = or_client.environments.get(name="myenvironment", base_url="http://localhost:8080")
Run:
python test_environment.py
Output:
Found 2 tasks
Prompt: What is 2+2?
Result: Correct!
Reward: 1.0

Local Development with Docker

Build and Run Locally

# Build Docker image
docker build -t my-environment:local .

# Run container
docker run -p 8080:8080 my-environment:local

# Test from another terminal
python test_environment.py

Next Steps