MCPcopy Create free account
hub / github.com/promptfoo/promptfoo / get_assert

Function get_assert

examples/integration-e2b/validate_and_run_code_e2b.py:190–284  ·  view source on GitHub ↗

Execute the generated function in E2B and compare stdout with expected output.

(output, context)

Source from the content-addressed store, hash-verified

188
189
190def get_assert(output, context):
191 """Execute the generated function in E2B and compare stdout with expected output."""
192 task_id = context.get("id", str(time.time()))
193 provider = context.get("provider", "unknown")
194 model = context.get("model", "unknown")
195
196 fn_name = context["vars"]["function_name"]
197 test_input = context["vars"]["test_input"]
198 expected = str(context["vars"]["expected_output"])
199
200 function_code = _extract_function(output, fn_name)
201 if not function_code:
202 snippet = output.strip()[:300].replace("\n", " ")
203 write_metrics(
204 task_id, provider, model, False, 0.0, extra={"reason": "no_code_found"}
205 )
206 return {
207 "pass": False,
208 "score": 0,
209 "reason": f"No Python code block found (first 300 chars: {snippet})",
210 }
211
212 # Pre-exec safety
213 if is_unsafe(function_code):
214 write_metrics(
215 task_id, provider, model, False, 0.0, extra={"reason": "unsafe_pattern"}
216 )
217 return {
218 "pass": False,
219 "score": 0,
220 "reason": "Unsafe pattern detected in generated code",
221 }
222
223 test_program = f"""{function_code}
224
225print({fn_name}({test_input}))
226"""
227
228 start = time.time()
229 with Sandbox.create() as sbx:
230 try:
231 res = _run_code_in_sandbox(sbx, test_program)
232 except Exception as e:
233 duration = time.time() - start
234 write_metrics(
235 task_id, provider, model, False, duration, extra={"error": str(e)}
236 )
237 return {
238 "pass": False,
239 "score": 0,
240 "reason": f"Sandbox execution error: {e}",
241 }
242
243 duration = time.time() - start
244 stdout, stderr = _stdout_from_result(res)
245 err_obj = getattr(res, "error", None)
246
247 if err_obj or stderr:

Callers

nothing calls this directly

Calls 7

write_metricsFunction · 0.90
_extract_functionFunction · 0.85
is_unsafeFunction · 0.85
_run_code_in_sandboxFunction · 0.85
_stdout_from_resultFunction · 0.85
getMethod · 0.45
createMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…