Skip to content

fix(openai_agents): keep raw input when function span input is not parseable JSON - #3365

Open
BlueX888 wants to merge 1 commit into
confident-ai:mainfrom
BlueX888:fix/prep-openai-agents-function-span-input-json-loads-crash
Open

BlueX888 wants to merge 1 commit into
confident-ai:mainfrom
BlueX888:fix/prep-openai-agents-function-span-input-json-loads-crash

Conversation

@BlueX888

Copy link
Copy Markdown

Summary

Function spans from the OpenAI Agents SDK whose input is not parseable JSON crashed DeepEvalTracingProcessor. The SDK emits such spans on real code paths: input=None for custom tools and apply_patch (both run through with_tool_function_span, which never sets input), and input="" for a computer call with no actions (_serialize_trace_payload(None) returns ""). The crash fired inside Observer.__exit__ — after the span had been added to trace_manager but before remove_span ran — so the ToolSpan leaked in active_spans under its placeholder "NA" name. The SDK's trace provider catches and logs processor exceptions, so nothing surfaced beyond the corrupted trace.

Why

deepeval/openai_agents/extractors.py:192 (before this change):

span.input = json.loads(function_span_data.input) or {
    "input": function_span_data.input
}

The SDK types FunctionSpanData.input as str | None (agents/tracing/span_data.py). json.loads(None) raises TypeError and json.loads("") raises json.JSONDecodeError. The call happens in Observer.__exit__ (deepeval/tracing/tracing.py:1272) — after trace_manager.add_span (deepeval/tracing/tracing.py:1212) and before trace_manager.remove_span (deepeval/tracing/tracing.py:1303) — so the exception aborted span finalization and left the span in active_spans. DefaultTraceProvider.on_span_end swallows processor exceptions, so the failure was silent.

Changes

  • deepeval/openai_agents/extractors.py — fall back to the raw input when json.loads raises, matching what the existing or {"input": ...} fallback already did for falsy parse results (e.g. "null").
  • tests/test_integrations/test_openai_agents/test_function_span_input.py — regression tests driving the same Observer wiring DeepEvalTracingProcessor uses, plus direct extractor-level fallback tests. All offline (no API key, no network).

Testing

New test file on the unpatched tree (the regression):

$ python -m pytest tests/test_integrations/test_openai_agents/test_function_span_input.py -v
test_input_none_does_not_raise FAILED
test_input_empty_string_does_not_raise FAILED
test_non_json_string_falls_back_to_raw FAILED
========================= 3 failed, 3 passed in 0.19s ==========================

test_input_none_does_not_raise fails with TypeError: the JSON object must be str, bytes or bytearray, not NoneType at extractors.py:192; the empty-string and non-JSON cases fail with json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0).

With this change:

$ python -m pytest tests/test_integrations/test_openai_agents/test_function_span_input.py
============================== 6 passed in 0.03s ===============================

Existing module tests:

$ python -m pytest tests/test_integrations/test_openai_agents/ -q
15 failed, 6 passed, 1 skipped in 0.93s

The 15 failures are byte-identical with and without this change and all fail with openai.OpenAIError: Missing credentials — these tests call the live OpenAI API (CONTRIBUTING.md notes they only fully run internally).

$ python -m black --check deepeval/openai_agents/extractors.py tests/test_integrations/test_openai_agents/test_function_span_input.py
All done! ✨ 🍰 ✨
2 files would be left unchanged.

$ python -m ruff check deepeval/openai_agents/extractors.py tests/test_integrations/test_openai_agents/test_function_span_input.py
All checks passed!

Note: verified with openai-agents 0.22.3. With the version pinned in poetry.lock (0.3.3), deepeval.openai_agents already fails to import on current main (deepeval/openai_agents/patch.py imports agents.run_internal, which does not exist in 0.3.3), so no test in this directory can run against the locked version — pre-existing and untouched by this change.

…rseable JSON

The agents SDK types FunctionSpanData.input as str | None and emits
function spans with input=None (custom tools / apply_patch, where
with_tool_function_span never sets it) and input="" (a computer call
with no actions, where _serialize_trace_payload(None) returns "").
Both made json.loads raise inside Observer.__exit__ — after the span
was added to trace_manager but before remove_span ran — so the
ToolSpan leaked in active_spans under its placeholder "NA" name, and
the SDK provider swallowed the exception so nothing surfaced.

Fall back to the raw value when parsing raises, matching what the
existing `or {"input": ...}` fallback already did for falsy parse
results.
@vercel

vercel Bot commented Sep 25, 2026

Copy link
Copy Markdown

@BlueX888 is attempting to deploy a commit to the Confident AI Team on Vercel.

A member of the Team first needs to authorize it.

This branch has not been deployed

No deployments
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