You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a rough proposal. It will be easier to clarify some details during implementation.
Problem
Users want to unit test Mirascope applications without making LLM calls. The current approach isn't documented or ergonomic, and we're missing key native functionality to make this a quality experience.
Solution
Add llm.TestProvider and an llm.test_provider context manager that intercepts all LLM calls at the provider level.
Why provider-level? The model.provider property is dynamic and looks up from the registry on every call. This means a registered TestProvider intercepts all calls, even from direct llm.Model() instantiation.
Proposed API
Basic Usage
frommirascopeimportllm# Handles setting the registry and cleaning it up so it's scoped to this contextwithllm.test_provider("openai/", responses=["Hello!"]) asprovider:
model=llm.Model("openai/gpt-4o")
response=model.call("Hi")
assertresponse.content=="Hello!"assertprovider.call_count==1
Agent Testing
frommirascopeimportllm@llm.tooldefget_weather(self) ->str:
returnf"72°F and sunny in {self.location}"@llm.agent("openai/gpt-4o", tools=[get_weather])defweather_agent(question: str) ->str:
returnquestion# Mock the full agent loop: tool call → final responsewithllm.test_provider("openai/", responses=[
llm.TestResponse(tool_calls=[get_weather(location="San Francisco")]),
llm.TestResponse(content="Based on the current conditions, it's 72°F and sunny in San Francisco. Great weather for a walk!"),
]):
result=weather_agent("What's the weather in SF?")
assert"72°F"inresultassert"sunny"inresult
Streaming
String responses auto-work for streaming:
withllm.test_provider("openai/", responses=["Hello!"]):
forchunkinmodel.stream("Hi"):
print(chunk.content) # Streams character by character# Or explicit chunks for fine-grained controlwithllm.test_provider("openai/", responses=[
llm.TestResponse(chunks=["Hello", " world", "!"]),
]):
chunks= [c.contentforcinmodel.stream("Hi")]
assertchunks== ["Hello", " world", "!"]
Description
Note
This is a rough proposal. It will be easier to clarify some details during implementation.
Problem
Users want to unit test Mirascope applications without making LLM calls. The current approach isn't documented or ergonomic, and we're missing key native functionality to make this a quality experience.
Solution
Add
llm.TestProviderand anllm.test_providercontext manager that intercepts all LLM calls at the provider level.Why provider-level? The
model.providerproperty is dynamic and looks up from the registry on every call. This means a registeredTestProviderintercepts all calls, even from directllm.Model()instantiation.Proposed API
Basic Usage
Agent Testing
Streaming
String responses auto-work for streaming:
Response Factory
Scope
llm.TestProvider- Provider returning mock responsesllm.test_provider- Context manager with automatic cleanupllm.TestResponse- Full response mocking (content, tool calls, chunks)call_count,calls,last_messages