MCPcopy Index your code
hub / github.com/promptfoo/promptfoo / handlePython

Function handlePython

src/assertions/python.ts:32–65  ·  view source on GitHub ↗
({
  assertion,
  renderedValue,
  valueFromScript,
  assertionValueContext,
  inverse,
  output,
}: AssertionParams)

Source from the content-addressed store, hash-verified

30}
31
32export const handlePython = async ({
33 assertion,
34 renderedValue,
35 valueFromScript,
36 assertionValueContext,
37 inverse,
38 output,
39}: AssertionParams): Promise<GradingResult> => {
40 invariant(typeof renderedValue === 'string', 'python assertion must have a string value');
41 try {
42 const result: ScriptAssertionResult =
43 typeof valueFromScript === 'undefined'
44 ? await runPythonCode(buildPythonScript(renderedValue), 'main', [
45 output,
46 assertionValueContext,
47 ])
48 : valueFromScript;
49
50 return normalizeScriptResult(
51 assertion,
52 result,
53 inverse,
54 { code: 'Python code', language: 'Python' },
55 assertion.value,
56 );
57 } catch (err) {
58 return {
59 pass: false,
60 score: 0,
61 reason: `Python code execution failed: ${(err as Error).message}`,
62 assertion,
63 };
64 }
65};

Callers

nothing calls this directly

Calls 4

runPythonCodeFunction · 0.90
normalizeScriptResultFunction · 0.90
invariantFunction · 0.85
buildPythonScriptFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…