Skip to content

Commit 1f8e70d

Browse files
authored
Python: Added internal kwargs filtering for Anthropic client (#3544)
* Added internal kwargs filtering for chat clients * Small updates * Reverted changes * Small fix * Fixed test
1 parent 3dc59c8 commit 1f8e70d

3 files changed

Lines changed: 38 additions & 2 deletions

File tree

python/packages/anthropic/agent_framework_anthropic/_chat_client.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,13 @@ def _prepare_options(
455455
# Add the structured outputs beta flag
456456
run_options["betas"].add(STRUCTURED_OUTPUTS_BETA_FLAG)
457457

458-
run_options.update(kwargs)
458+
# Filter out framework kwargs that should not be passed to the Anthropic API.
459+
# This includes underscore-prefixed internal objects (like _function_middleware_pipeline)
460+
# and framework kwargs like 'thread' and 'middleware'.
461+
filtered_kwargs = {
462+
k: v for k, v in kwargs.items() if not k.startswith("_") and k not in {"thread", "middleware"}
463+
}
464+
run_options.update(filtered_kwargs)
459465
return run_options
460466

461467
def _prepare_betas(self, options: Mapping[str, Any]) -> set[str]:

python/packages/anthropic/tests/test_anthropic_client.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,36 @@ async def test_prepare_options_with_top_p(mock_anthropic_client: MagicMock) -> N
479479
assert run_options["top_p"] == 0.9
480480

481481

482+
async def test_prepare_options_filters_internal_kwargs(mock_anthropic_client: MagicMock) -> None:
483+
"""Test _prepare_options filters internal framework kwargs.
484+
485+
Internal kwargs like _function_middleware_pipeline, thread, and middleware
486+
should be filtered out before being passed to the Anthropic API.
487+
"""
488+
chat_client = create_test_anthropic_client(mock_anthropic_client)
489+
490+
messages = [ChatMessage(role="user", text="Hello")]
491+
chat_options: ChatOptions = {}
492+
493+
# Simulate internal kwargs that get passed through the middleware pipeline
494+
internal_kwargs = {
495+
"_function_middleware_pipeline": object(),
496+
"_chat_middleware_pipeline": object(),
497+
"_any_underscore_prefixed": object(),
498+
"thread": object(),
499+
"middleware": [object()],
500+
}
501+
502+
run_options = chat_client._prepare_options(messages, chat_options, **internal_kwargs)
503+
504+
# Internal kwargs should be filtered out
505+
assert "_function_middleware_pipeline" not in run_options
506+
assert "_chat_middleware_pipeline" not in run_options
507+
assert "_any_underscore_prefixed" not in run_options
508+
assert "thread" not in run_options
509+
assert "middleware" not in run_options
510+
511+
482512
# Response Processing Tests
483513

484514

python/packages/core/tests/workflow/test_sub_workflow.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ async def test_sub_workflow_checkpoint_restore_no_duplicate_requests() -> None:
599599

600600
# Get checkpoint
601601
checkpoints = await storage.list_checkpoints(workflow1.id)
602-
checkpoint_id = max(checkpoints, key=lambda cp: cp.timestamp).checkpoint_id
602+
checkpoint_id = max(checkpoints, key=lambda cp: cp.iteration_count).checkpoint_id
603603

604604
# Step 2: Resume workflow from checkpoint
605605
workflow2 = _build_checkpoint_test_workflow(storage)

0 commit comments

Comments
 (0)