openai-agents-python/tests/models/test_litellm_extra_body.py
Rohan Mehta d4c7a23e1d
Don't cache agent tools during a run (#803)
### Summary:
Towards #767. We were caching the list of tools for an agent, so if you
did `agent.tools.append(...)` from a tool call, the next call to the
model wouldn't include the new tool. THis is a bug.

### Test Plan:
Unit tests. Note that now MCP tools are listed each time the agent runs
(users can still cache the `list_tools` however).
2025-06-02 14:49:16 -04:00

44 lines
1.4 KiB
Python

import litellm
import pytest
from litellm.types.utils import Choices, Message, ModelResponse, Usage
from agents.extensions.models.litellm_model import LitellmModel
from agents.model_settings import ModelSettings
from agents.models.interface import ModelTracing
@pytest.mark.allow_call_model_methods
@pytest.mark.asyncio
async def test_extra_body_is_forwarded(monkeypatch):
"""
Forward `extra_body` entries into litellm.acompletion kwargs.
This ensures that user-provided parameters (e.g. cached_content)
arrive alongside default arguments.
"""
captured: dict[str, object] = {}
async def fake_acompletion(model, messages=None, **kwargs):
captured.update(kwargs)
msg = Message(role="assistant", content="ok")
choice = Choices(index=0, message=msg)
return ModelResponse(choices=[choice], usage=Usage(0, 0, 0))
monkeypatch.setattr(litellm, "acompletion", fake_acompletion)
settings = ModelSettings(
temperature=0.1, extra_body={"cached_content": "some_cache", "foo": 123}
)
model = LitellmModel(model="test-model")
await model.get_response(
system_instructions=None,
input=[],
model_settings=settings,
tools=[],
output_schema=None,
handoffs=[],
tracing=ModelTracing.DISABLED,
previous_response_id=None,
)
assert {"cached_content": "some_cache", "foo": 123}.items() <= captured.items()