Commit 73ad6e8
Python: Fix: Implement __deepcopy__ on KernelFunction to handle non-serializable OTEL metrics (#12803)
### Description
This PR fixes TypeError: cannot pickle '_thread.RLock' object which
occurs when kernel.clone() is called while OpenTelemetry metrics are
enabled.
- Fixes: #12802
### Motivation and Context
When using features like `HandoffOrchestration`, the agent's `Kernel` is
cloned. This process uses `copy.deepcopy`, which fails on the
`KernelFunction`'s OpenTelemetry `Histogram` fields
(`invocation_duration_histogram` and `streaming_duration_histogram`).
These histogram objects contain thread locks, making them
non-serializable.
Previous solutions, like the one in **PR #9292**, used
`Field(exclude=True, default_factory=...)`. While that was a necessary
step, it was insufficient because `deepcopy` does not respect the
`exclude` flag in the same way that Pydantic's serialization does. This
bug blocks the use of certain agentic features in any application that
has metrics-based observability enabled.
### Description of the change
This solution introduces a custom __deepcopy__ method to the
KernelFunction Pydantic model. This method correctly handles the
deep-copying process for KernelFunction instances:
1. It intercepts the deepcopy call for KernelFunction objects.
2. It uses self.model_copy(deep=False) to create a new instance of the
Pydantic model. Using deep=False allows the default_factory for the
histogram fields to create new, clean instances on the copied object,
while other fields are shallow-copied.
3. It then manually deep-copies all other attributes from the original
object to the new one, ensuring that any other mutable objects (like
metadata`) are correctly duplicated. The histogram fields are explicitly
skipped in this step, as they have already been recreated by
`model_copy.
4. Standard deepcopy memoization is used to prevent infinite recursion
in the case of cyclic object graphs.
This approach ensures that the non-serializable fields are
re-initialized instead of copied, resolving the TypeError while
maintaining the integrity of the cloned object.
### How has this been tested?
The fix was validated using the reproduction steps outlined in the
associated issue. Running an agent orchestration that triggers
kernel.clone() with OpenTelemetry metrics enabled now completes
successfully without any errors.
### Contribution Checklist
<!-- Before submitting this PR, please make sure: -->
- [X] The code builds clean without any errors or warnings
- [X] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [X] All unit tests pass, and I have added new tests where possible
- [X] I didn't break anyone 😄
---------
Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>1 parent a6b5f34 commit 73ad6e8
File tree
7 files changed
+1313
-1069
lines changed- python
- semantic_kernel
- connectors
- functions
- tests/unit/functions
7 files changed
+1313
-1069
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
675 | 675 | | |
676 | 676 | | |
677 | 677 | | |
678 | | - | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
679 | 690 | | |
680 | 691 | | |
681 | 692 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
422 | 422 | | |
423 | 423 | | |
424 | 424 | | |
425 | | - | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
426 | 433 | | |
427 | 434 | | |
428 | 435 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
637 | 637 | | |
638 | 638 | | |
639 | 639 | | |
640 | | - | |
641 | | - | |
642 | | - | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
643 | 645 | | |
644 | 646 | | |
645 | 647 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
469 | 469 | | |
470 | 470 | | |
471 | 471 | | |
472 | | - | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
473 | 480 | | |
474 | 481 | | |
475 | 482 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
103 | 122 | | |
104 | 123 | | |
105 | 124 | | |
| |||
Lines changed: 20 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
626 | 627 | | |
627 | 628 | | |
628 | 629 | | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
0 commit comments