|
| 1 | +import litellm |
| 2 | +import pytest |
| 3 | +from litellm.types.utils import Choices, Message, ModelResponse, Usage |
| 4 | +from openai.types.chat.chat_completion import ChatCompletion, Choice |
| 5 | +from openai.types.chat.chat_completion_message import ChatCompletionMessage |
| 6 | +from openai.types.completion_usage import CompletionUsage |
| 7 | + |
| 8 | +from agents.extensions.models.litellm_model import LitellmModel |
| 9 | +from agents.model_settings import ModelSettings |
| 10 | +from agents.models.interface import ModelTracing |
| 11 | +from agents.models.openai_chatcompletions import OpenAIChatCompletionsModel |
| 12 | + |
| 13 | + |
| 14 | +@pytest.mark.allow_call_model_methods |
| 15 | +@pytest.mark.asyncio |
| 16 | +async def test_litellm_kwargs_forwarded(monkeypatch): |
| 17 | + """ |
| 18 | + Test that kwargs from ModelSettings are forwarded to litellm.acompletion. |
| 19 | + """ |
| 20 | + captured: dict[str, object] = {} |
| 21 | + |
| 22 | + async def fake_acompletion(model, messages=None, **kwargs): |
| 23 | + captured.update(kwargs) |
| 24 | + msg = Message(role="assistant", content="test response") |
| 25 | + choice = Choices(index=0, message=msg) |
| 26 | + return ModelResponse(choices=[choice], usage=Usage(0, 0, 0)) |
| 27 | + |
| 28 | + monkeypatch.setattr(litellm, "acompletion", fake_acompletion) |
| 29 | + |
| 30 | + settings = ModelSettings( |
| 31 | + temperature=0.5, |
| 32 | + kwargs={ |
| 33 | + "custom_param": "custom_value", |
| 34 | + "seed": 42, |
| 35 | + "stop": ["END"], |
| 36 | + "logit_bias": {123: -100}, |
| 37 | + }, |
| 38 | + ) |
| 39 | + model = LitellmModel(model="test-model") |
| 40 | + |
| 41 | + await model.get_response( |
| 42 | + system_instructions=None, |
| 43 | + input="test input", |
| 44 | + model_settings=settings, |
| 45 | + tools=[], |
| 46 | + output_schema=None, |
| 47 | + handoffs=[], |
| 48 | + tracing=ModelTracing.DISABLED, |
| 49 | + previous_response_id=None, |
| 50 | + ) |
| 51 | + |
| 52 | + # Verify that all kwargs were passed through |
| 53 | + assert captured["custom_param"] == "custom_value" |
| 54 | + assert captured["seed"] == 42 |
| 55 | + assert captured["stop"] == ["END"] |
| 56 | + assert captured["logit_bias"] == {123: -100} |
| 57 | + |
| 58 | + # Verify regular parameters are still passed |
| 59 | + assert captured["temperature"] == 0.5 |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.allow_call_model_methods |
| 63 | +@pytest.mark.asyncio |
| 64 | +async def test_openai_chatcompletions_kwargs_forwarded(monkeypatch): |
| 65 | + """ |
| 66 | + Test that kwargs from ModelSettings are forwarded to OpenAI chat completions API. |
| 67 | + """ |
| 68 | + captured: dict[str, object] = {} |
| 69 | + |
| 70 | + class MockChatCompletions: |
| 71 | + async def create(self, **kwargs): |
| 72 | + captured.update(kwargs) |
| 73 | + msg = ChatCompletionMessage(role="assistant", content="test response") |
| 74 | + choice = Choice(index=0, message=msg, finish_reason="stop") |
| 75 | + return ChatCompletion( |
| 76 | + id="test-id", |
| 77 | + created=0, |
| 78 | + model="gpt-4", |
| 79 | + object="chat.completion", |
| 80 | + choices=[choice], |
| 81 | + usage=CompletionUsage(completion_tokens=5, prompt_tokens=10, total_tokens=15), |
| 82 | + ) |
| 83 | + |
| 84 | + class MockChat: |
| 85 | + def __init__(self): |
| 86 | + self.completions = MockChatCompletions() |
| 87 | + |
| 88 | + class MockClient: |
| 89 | + def __init__(self): |
| 90 | + self.chat = MockChat() |
| 91 | + self.base_url = "https://api.openai.com/v1" |
| 92 | + |
| 93 | + settings = ModelSettings( |
| 94 | + temperature=0.7, |
| 95 | + kwargs={"seed": 123, "logit_bias": {456: 10}, "stop": ["STOP", "END"], "user": "test-user"}, |
| 96 | + ) |
| 97 | + |
| 98 | + mock_client = MockClient() |
| 99 | + model = OpenAIChatCompletionsModel(model="gpt-4", openai_client=mock_client) # type: ignore |
| 100 | + |
| 101 | + await model.get_response( |
| 102 | + system_instructions="Test system", |
| 103 | + input="test input", |
| 104 | + model_settings=settings, |
| 105 | + tools=[], |
| 106 | + output_schema=None, |
| 107 | + handoffs=[], |
| 108 | + tracing=ModelTracing.DISABLED, |
| 109 | + previous_response_id=None, |
| 110 | + ) |
| 111 | + |
| 112 | + # Verify that all kwargs were passed through |
| 113 | + assert captured["seed"] == 123 |
| 114 | + assert captured["logit_bias"] == {456: 10} |
| 115 | + assert captured["stop"] == ["STOP", "END"] |
| 116 | + assert captured["user"] == "test-user" |
| 117 | + |
| 118 | + # Verify regular parameters are still passed |
| 119 | + assert captured["temperature"] == 0.7 |
| 120 | + |
| 121 | + |
| 122 | +@pytest.mark.allow_call_model_methods |
| 123 | +@pytest.mark.asyncio |
| 124 | +async def test_empty_kwargs_handling(monkeypatch): |
| 125 | + """ |
| 126 | + Test that empty or None kwargs are handled gracefully. |
| 127 | + """ |
| 128 | + captured: dict[str, object] = {} |
| 129 | + |
| 130 | + async def fake_acompletion(model, messages=None, **kwargs): |
| 131 | + captured.update(kwargs) |
| 132 | + msg = Message(role="assistant", content="test response") |
| 133 | + choice = Choices(index=0, message=msg) |
| 134 | + return ModelResponse(choices=[choice], usage=Usage(0, 0, 0)) |
| 135 | + |
| 136 | + monkeypatch.setattr(litellm, "acompletion", fake_acompletion) |
| 137 | + |
| 138 | + # Test with None kwargs |
| 139 | + settings_none = ModelSettings(temperature=0.5, kwargs=None) |
| 140 | + model = LitellmModel(model="test-model") |
| 141 | + |
| 142 | + await model.get_response( |
| 143 | + system_instructions=None, |
| 144 | + input="test input", |
| 145 | + model_settings=settings_none, |
| 146 | + tools=[], |
| 147 | + output_schema=None, |
| 148 | + handoffs=[], |
| 149 | + tracing=ModelTracing.DISABLED, |
| 150 | + previous_response_id=None, |
| 151 | + ) |
| 152 | + |
| 153 | + # Should work without error and include regular parameters |
| 154 | + assert captured["temperature"] == 0.5 |
| 155 | + |
| 156 | + # Test with empty dict |
| 157 | + captured.clear() |
| 158 | + settings_empty = ModelSettings(temperature=0.3, kwargs={}) |
| 159 | + |
| 160 | + await model.get_response( |
| 161 | + system_instructions=None, |
| 162 | + input="test input", |
| 163 | + model_settings=settings_empty, |
| 164 | + tools=[], |
| 165 | + output_schema=None, |
| 166 | + handoffs=[], |
| 167 | + tracing=ModelTracing.DISABLED, |
| 168 | + previous_response_id=None, |
| 169 | + ) |
| 170 | + |
| 171 | + # Should work without error and include regular parameters |
| 172 | + assert captured["temperature"] == 0.3 |
0 commit comments