Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(evals): return INVALID_RESULT for cyclic Python evaluator results (#18386)

Tobias Wochingerd54b78efec31
diff --git a/scripts/code-eval-runners/python/code_based_eval_handler.py b/scripts/code-eval-runners/python/code_based_eval_handler.py
index bd19481da5..32cc3c32ae 100644
--- a/scripts/code-eval-runners/python/code_based_eval_handler.py
+++ b/scripts/code-eval-runners/python/code_based_eval_handler.py
@@ -125,7 +125,15 @@ def handler(event, context):
except Exception as error:
return runner_error("USER_CODE_ERROR", format_error(error))
- return normalize_result(result)
+ # Cycles raise RecursionError, or ValueError from json.dumps.
+ try:
+ return normalize_result(result)
+ except (RecursionError, ValueError) as error:
+ return runner_error(
+ "INVALID_RESULT",
+ "Evaluator returned a result that could not be serialized: "
+ f"{format_error(error)}",
+ )
def normalize_result(result):
diff --git a/worker/src/features/evaluation/codeBased/awsLambdaCodeEvalDispatcher.integration.test.ts b/worker/src/features/evaluation/codeBased/awsLambdaCodeEvalDispatcher.integration.test.ts
index 02b819999d..bb2fd3e5b5 100644
--- a/worker/src/features/evaluation/codeBased/awsLambdaCodeEvalDispatcher.integration.test.ts
+++ b/worker/src/features/evaluation/codeBased/awsLambdaCodeEvalDispatcher.integration.test.ts
@@ -147,6 +147,47 @@ describeWithFloci("AwsLambdaCodeEvalDispatcher Floci integration", () => {
FLOCI_TEST_TIMEOUT_MS,
);
+ it.each([
+ {
+ label: "self-referential list",
+ source: `
+def evaluate(ctx):
+ items = []
+ items.append(items)
+ return {"scores": [{"name": "x", "value": 1, "metadata": {"items": items}}]}
+`,
+ message: /RecursionError/,
+ },
+ {
+ label: "tuple holding a cyclic list",
+ source: `
+def evaluate(ctx):
+ items = []
+ items.append(items)
+ return {"scores": [{"name": "x", "value": 1, "metadata": {"items": (items,)}}]}
+`,
+ message: /Circular reference detected/,
+ },
+ ])(
+ "returns a non-retryable INVALID_RESULT for a Python $label",
+ async ({ source, message }) => {
+ // Cycles must not escape the runner. An unhandled Lambda error is
+ // classified as a retryable invocation failure.
+ await expect(
+ dispatcher.dispatch({
+ ...baseInput,
+ runtime: { language: "PYTHON" },
+ code: { source },
+ }),
+ ).rejects.toMatchObject({
+ code: "INVALID_RESULT",
+ message: expect.stringMatching(message),
+ retryable: false,
+ } satisfies Partial<CodeEvalDispatcherError>);
+ },
+ FLOCI_TEST_TIMEOUT_MS,
+ );
+
it(
"formats Python exceptions with type and evaluator line number",
async () => {