@@ -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
0 commit comments