Skip to main content
This example demonstrates a multi-turn game environment where models play Wordle by making guesses and receiving feedback. It showcases parsing structured output, custom reward functions, and integration with TextArena.

Overview

The Wordle environment provides:
  • Game: 5-letter word guessing with color-coded feedback
  • Format: Multi-turn interaction (up to 6 guesses)
  • Parsing: XML tags for structured guess extraction
  • Rewards: Correctness, efficiency bonus, and partial credit
  • Integration: TextArena game library

Complete Implementation

Here’s the full working implementation from environments/wordle/wordle.py:

How It Works

1. TextArena Integration

TextArenaEnv wraps TextArena games for RL training:
TextArena provides text-based game environments. The Wordle game:
  • Generates random 5-letter target words
  • Accepts guesses and returns color-coded feedback
  • Tracks game state (remaining guesses, history)

2. Structured Output Parsing

XMLParser extracts guesses from model responses:
Example parsing:

3. Feedback Processing

The wordle_feedback_fn extracts game feedback:
Example feedback:
Legend:
  • G = Green (correct letter, correct position)
  • Y = Yellow (correct letter, wrong position)
  • _ = Gray (letter not in word)

4. Multi-Part Reward Function

Three reward components encourage different behaviors:
Returns: 1.0 for correct word, 0.0 otherwise

Combined Reward

Total reward = weighted sum of all components

Example Game Trajectory

Target word: STEAK
Turn 1Assistant: I’ll start with a common word to test vowels and common consonants. CRANEEnvironment:

Turn 2Assistant: A is in position 3 (wrong spot), E is in position 5 (wrong spot). Let me try: STEALEnvironment:

Turn 3Assistant: Almost there! The first four letters are S-T-E-A. Last letter isn’t L. Let me try: STEAKEnvironment:
Rewards:
  • correct_answer: 1.0
  • length_bonus: 1.0 / 3 = 0.33
  • partial_answer: 0.0 (already correct)
  • format_reward: 1.0
  • Total: 2.53

Running the Environment

Installation

Quick Evaluation

Training Dataset

Configuration Options

Key Features

Structured Output with XMLParser

XMLParser provides:
  • Extraction: Pulls content from XML tags
  • Validation: Checks format compliance
  • Format rewards: Built-in reward function for proper formatting

Multi-Component Rewards

Combining multiple reward signals:
  • Sparse signal (correct_answer): Only 1.0 when winning
  • Dense signal (partial_answer): Credit for progress
  • Efficiency (length_bonus): Reward faster solutions
  • Compliance (format_reward): Enforce output format
This creates a rich learning signal for RL training.

Game State Tracking

TextArenaEnv automatically tracks:
  • Number of guesses made
  • Guess history
  • Remaining attempts
  • Win/loss status

Metrics Tracked

  • correct_answer: 1.0 if word guessed correctly
  • length_bonus: Efficiency bonus (0.0 to 1.0)
  • partial_answer: Progress score (0.0 to 1.0)
  • format_reward: Format compliance (0.0 or 1.0)
  • reward: Combined weighted sum
  • num_turns: Number of guesses made

Advanced Usage

Custom Reward Weights

Adjust the importance of different reward components:

Different Wordle Variants

TextArena supports multiple Wordle variants:

Other TextArena Games

The same pattern works for other TextArena games:

Next Steps