Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/source/environments/textarena.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ The server supports configuration via environment variables:
- `TEXTARENA_NUM_PLAYERS` - Number of players (default: 1)
- `TEXTARENA_MAX_TURNS` - Maximum turns per episode
- `TEXTARENA_DOWNLOAD_NLTK` - Download NLTK data (default: "1")
- `TEXTARENA_KW_*` - Pass additional kwargs to TextArena (e.g., `TEXTARENA_KW_difficulty=hard`)
- `TEXTARENA_KW_*` - Pass additional kwargs to TextArena. Values are read as JSON, so numbers and booleans keep their type (e.g., `TEXTARENA_KW_CLUES=65` for Sudoku, `TEXTARENA_KW_difficulty=hard`)

## Development & Testing

Expand Down
2 changes: 1 addition & 1 deletion envs/textarena_env/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ The server supports configuration via environment variables:
- `TEXTARENA_NUM_PLAYERS` - Number of players (default: 1)
- `TEXTARENA_MAX_TURNS` - Maximum turns per episode
- `TEXTARENA_DOWNLOAD_NLTK` - Download NLTK data (default: "1")
- `TEXTARENA_KW_*` - Pass additional kwargs to TextArena (e.g., `TEXTARENA_KW_difficulty=hard`)
- `TEXTARENA_KW_*` - Pass additional kwargs to TextArena. Values are read as JSON, so numbers and booleans keep their type (e.g., `TEXTARENA_KW_CLUES=65` for Sudoku, `TEXTARENA_KW_difficulty=hard`)

## Development & Testing

Expand Down
17 changes: 13 additions & 4 deletions envs/textarena_env/server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
from __future__ import annotations

import inspect
import json
import logging
import os
from typing import Any

from openenv.core.env_server.http_server import create_app

Expand All @@ -31,14 +33,21 @@
from .gradio_ui import build_textarena_gradio_app


def _parse_env_kwargs(prefix: str = "TEXTARENA_KW_") -> dict[str, str]:
"""Collect arbitrary environment kwargs from the process environment."""
def _parse_env_kwargs(prefix: str = "TEXTARENA_KW_") -> dict[str, Any]:
"""Collect arbitrary environment kwargs from the process environment.

env_kwargs: dict[str, str] = {}
Values are read as JSON, so `TEXTARENA_KW_CLUES=65` passes the integer 65. Values that are not JSON, like
`TEXTARENA_KW_DIFFICULTY=hard`, are passed as strings.
"""

env_kwargs: dict[str, Any] = {}
for key, value in os.environ.items():
if key.startswith(prefix):
env_key = key[len(prefix) :].lower()
env_kwargs[env_key] = value
try:
env_kwargs[env_key] = json.loads(value)
except json.JSONDecodeError:
env_kwargs[env_key] = value
return env_kwargs


Expand Down
22 changes: 22 additions & 0 deletions tests/envs/test_textarena_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,3 +536,25 @@ def test_bandit_seeded_trajectory_is_reproducible_across_sessions():

assert sum(len(v) for v in _bandit_history(first).values()) == len(actions)
assert _bandit_history(first) == _bandit_history(second)


def test_env_kwargs_are_parsed_as_json(monkeypatch):
pytest.importorskip("textarena", reason="textarena not installed")
from textarena_env.server.app import _parse_env_kwargs

monkeypatch.setenv("TEXTARENA_KW_CLUES", "65")
monkeypatch.setenv("TEXTARENA_KW_DIFFICULTY", "hard")
monkeypatch.setenv("TEXTARENA_KW_HARDCORE", "true")

assert _parse_env_kwargs() == {"clues": 65, "difficulty": "hard", "hardcore": True}


def test_sudoku_clues_from_env_kwargs(monkeypatch):
pytest.importorskip("textarena", reason="textarena not installed")
from textarena_env.server.app import _parse_env_kwargs

monkeypatch.setenv("TEXTARENA_KW_CLUES", "65")
env = TextArenaEnvironment(env_id="Sudoku-v0", env_kwargs=_parse_env_kwargs())
board = env.reset().messages[0].content.split("Game Board:")[-1]

assert board.count(".") <= 81 - 65
Loading