#10502·dspy

Evaluate(display_table=True) crashes with UnicodeEncodeError from stylize_metric_name's checkmark on non-UTF-8 stdout

Author: VANDRANKICreated Sep 25, 2026Updated Sep 25, 2026

What happens

dspy.Evaluate(...)(program, display_table=True) (or any truthy display_table outside a notebook) can crash with UnicodeEncodeError after the evaluation has already finished computing the score. The crash happens in the results-table print step, not in scoring, so the caller loses the EvaluationResult even though the run itself succeeded.

Root cause

dspy/evaluate/evaluate.py's stylize_metric_name hardcodes a checkmark symbol into every cell of the metric column:

python
def format_metric(x):
    if isinstance(x, float):
        return f"[CHECKMARK] [{x:.3f}]"
    elif x is not None:
        return f"[CHECKMARK] [{x}]"
    else:
        return ""
df[metric_name] = df[metric_name].apply(format_metric)

(the real source uses the literal two-codepoint emoji U+2714 U+FE0F, "heavy check mark" + variation selector-16, in place of [CHECKMARK] above)

_display_result_table then calls display_dataframe(df_to_display), which for a non-notebook environment does a plain print(df):

python
def display_dataframe(df: "pd.DataFrame"):
    ...
    else:
        with pd.option_context("display.max_rows", None, "display.max_columns", None):
            print(df)

print() encodes to sys.stdout.encoding. On Windows, the default console/subprocess encoding is the ANSI code page (cp1252 on Western installs), which cannot represent U+2714 U+FE0F. This raises UnicodeEncodeError and propagates out of Evaluate.__call__, so the caller never gets the EvaluationResult even though ncorrect/ntotal were already computed and logged correctly.

This reproduces for every evaluation regardless of devset content: the emoji is added by dspy itself, not by user data, so a devset of plain ASCII strings still crashes.

There is already a TODO/FIXME at the bottom of the same file flagging uncertainty about exactly this code path:

python
# FIXME: TODO: The merge_dicts stuff above is way too quick and dirty.
# TODO: the display_table can't handle False but can handle 0!
# Not sure how it works with True exactly, probably fails too.

This confirms the suspicion: display_table=True does fail, via this Unicode crash.

Repro

Tested against dspy 3.4.0 (source verified byte-identical to current main at dspy/evaluate/evaluate.py), on Windows 11 with sys.stdout.encoding == "cp1252":

python
import dspy
from dspy.evaluate import Evaluate

devset = [dspy.Example(q="1", a="1").with_inputs("q")]

class Echo(dspy.Module):
    def forward(self, q):
        return dspy.Prediction(a=q)

def metric(example, pred, trace=None):
    return 1.0 if example.a == pred.a else 0.0

ev = Evaluate(devset=devset, metric=metric, display_progress=False, display_table=True)
ev(Echo())

Output:

2026/09/25 ... INFO dspy.evaluate.evaluate: Average Metric: 1.0 / 1 (100.0%)
Traceback (most recent call last):
  ...
  File "...\dspy\evaluate\evaluate.py", line 194, in __call__
    self._display_result_table(result_df, display_table, metric_name)
  File "...\dspy\evaluate\evaluate.py", line 287, in _display_result_table
    display_dataframe(df_to_display)
  File "...\dspy\evaluate\evaluate.py", line 374, in display_dataframe
    print(df)
  File "...\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 58-59: character maps to <undefined>

I isolated it further to confirm it is exactly the checkmark character, nothing else about the DataFrame:

python
import pandas as pd
df = pd.DataFrame({"metric": ["✔️ [1.000]"]})
print(df)   # UnicodeEncodeError: 'charmap' codec can't encode characters ...

Expected vs actual

Expected: display_table=True prints a results table and Evaluate.__call__ returns the EvaluationResult normally.

Actual: it raises UnicodeEncodeError on any console/subprocess whose stdout is not UTF-8 capable (default Windows consoles, and non-Windows environments without a UTF-8 locale), and the EvaluationResult is never returned to the caller.

Suggested fix

Either drop the emoji (plain text like f"[{x:.3f}]" conveys the same information without depending on stdout's codec), or make the print(df) call encoding-safe.

This is on the current main branch, dspy/evaluate/evaluate.py, functions stylize_metric_name and display_dataframe.

Duplicate check

I searched existing issues/PRs first. #10411 / #10419 are a different, already-tracked bug in the same file (missing encoding="utf-8" on the save_as_csv/save_as_json file writes). #990 and #1682 are older, already-merged fixes for two different display_table bugs (an isinstance(True, int) indexing bug, and a dict-coercion bug) that are not present in the current code. None of these cover this checkmark/console-print crash.