Skip to content

Return serialization failures from query_raw normally - #30

Merged
eliotwrobson merged 1 commit into
mainfrom
claude/query-not-serializable
Nov 5, 2026
Merged

eliotwrobson merged 1 commit into
mainfrom
claude/query-not-serializable

Conversation

@eliotwrobson

Copy link
Copy Markdown
Collaborator

Problem

When a student variable couldn't be serialized, sandbox.query failed with KeyError: 'value'. The sandbox sent a failure message without the usual fields and closed the connection, so later queries failed with Connection closed by peer before termination character was found. This is the same problem #29 fixed for query_function.

Changes

  • The sandbox now serializes inside a try, covering both to_json and json.dumps, and stays connected.
  • query_raw returns QueryStatusCode.NOT_SERIALIZABLE with error set to a message like Variable 'x' has a value of type Thing, which can't be sent back to the grader: ....
  • query raises a RuntimeError with that message, so students see it through assert_equal(sandbox.query("x_sq"), ...).
  • StudentQueryResponse gains an error field, which is None otherwise.
  • The quick_start.md section on query now describes these errors. It also said a missing variable raises RuntimeError, but it raises NameError, so I corrected that.

The status name and error field match #27's query_setup response.

Testing

  • New test_query_not_serializable scenario covers the raw response, nested and self-referencing values, query, assert_equal, missing variables and later queries.
  • uv run pytest tests -p no:prairielearn-grader on Python 3.14: all 72 passed on the second run. The first run in a fresh environment failed only test_matplotlib_plots, a first-run failure also seen without this change.
  • mypy src/ is clean, and ruff check shows nothing new.
  • Lite image built from this branch's docker/ files, with this branch's wheel installed over the PyPI package. I confirmed the installed package contains the new message. Graded with PrairieLearn's square tests from its convert-python-square branch, as ag, with a hand-written data.json:
Submission Score Feedback
Correct x_sq 1.0 None
Wrong x_sq 0.0 Usual expected and actual output
x_sq = Thing() 0.0 Variable 'x_sq' has a value of type Thing, which can't be sent back to the grader: ...
  • With x = Thing() and y = 5 and a hand-written test, also in that image: query_raw("x") returned the new status, query("x") raised the message, and query("y") then returned 5.

The full image wasn't built.

🤖 Generated with Claude Code

When a student variable couldn't be serialized, the sandbox sent a failure
message without the usual response fields and closed the connection. query
then failed with KeyError: 'value', and later queries crashed with socket
errors.

The sandbox now serializes inside a try and sends a normal response with
QueryStatusCode.NOT_SERIALIZABLE and an error naming the variable and its type,
and keeps the connection open. query raises a RuntimeError with that message.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@eliotwrobson
eliotwrobson marked this pull request as ready for review November 5, 2026 18:30
@eliotwrobson
eliotwrobson merged commit 0e47131 into main Nov 5, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant