Python: fix circular reference error in KernelArguments.dumps() during OTel diagnostics#13642
Open
MaxwellCalkin wants to merge 1 commit intomicrosoft:mainfrom
Open
Conversation
…g OTel diagnostics When SEMANTICKERNEL_EXPERIMENTAL_GENAI_ENABLE_OTEL_DIAGNOSTICS_SENSITIVE is enabled, KernelFunction.invoke() calls arguments.dumps() to serialize function arguments for OpenTelemetry spans. If the arguments contain objects with circular references (e.g., KernelProcessStepContext, whose step_message_channel holds back-references to the process graph), this causes json.dumps to raise 'Circular reference detected'. The fix adds defensive handling in KernelArguments.dumps(): - Track seen object IDs to detect circular references in the custom default serializer - Catch ValueError from json.dumps and fall back to per-key serialization, replacing un-serializable values with their type name Fixes microsoft#13393
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and Context
Fixes #13393
When running Process Framework steps with
SEMANTICKERNEL_EXPERIMENTAL_GENAI_ENABLE_OTEL_DIAGNOSTICS_SENSITIVE=true, every function invocation fails withCircular reference detected. This happens because:KernelFunction.invoke()callsarguments.dumps()to serialize function arguments for OpenTelemetry span attributesKernelProcessStepContextargument (injected byfind_input_channels()instep_utils.py)KernelProcessStepContextcontains astep_message_channelfield, which is theLocalStepinstance itselfLocalStepextendsKernelBaseModeland containsKernel,KernelFunctionobjects, output edges, and other complex runtime objects that form circular reference chainsdumps()callsmodel_dump()on the context, then passes the result tojson.dumps(), the circular references causeValueError: Circular reference detectedDescription
Makes
KernelArguments.dumps()resilient to circular references:defaultserializer now tracksid()of objects it has already visited, returning a safe placeholder string (<circular ref: ClassName>) instead of recursing into circular structuresmodel_dump()failures: Wrapsmodel_dump()in a try/except so that if a Pydantic model itself raises during serialization, the type name is used as a placeholderValueErrorcatch: Ifjson.dumps()still detects a circular reference (e.g., from nested dicts produced bymodel_dump()), falls back to per-key serialization where each un-serializable value is replaced with its type nameThis is a defensive fix at the serialization boundary — it ensures OTel diagnostics never crash function invocations, regardless of argument complexity.
Contribution Checklist
test_kernel_arguments.py