Responses¶
There are many ways to define the response for your mocked API request. To define a response you just need to set the response
property to either a partial, a model, an HTTPX response, or a function that returns an HTTPX response. You can also ignore setting the response and a default response will be used.
Default Response¶
For all routes, but especially stateful routes, you can skip manually defining the response and a default response will be returned. The default response will have all required fields of the return object but will not include any meaningful values for fields that would have been generated by the LLM.
Tip
For stateful routes that do not involve LLM generated fields it is actually recommended to not define the response. Doing so might actually result in an error.
Partial¶
All routes have an associated partial object. Partials are just typed dictionary representations of the OpenAI response object. Any field not defined by the user will be given a default value by merging the partial object with the default response object.
Let's look at an example:
In this example, we're explicitly defining what the completion choices
field should look like in the response but we're not explicitly defining any of the other fields.
Thanks to Python's TypedDict
type, autocompletion for field names are automatically supported in your text editor or IDE.
Model¶
Along with partial objects, you can also choose to set the response to the full OpenAI object.
One use case for this is to manually set the status
field on the run resource object for polling.
HTTPX Response¶
You can set the response to a raw HTTPX response object. This is more involved than using either a partial or model but can allow you to test things like server failures or other status codes.
Tip
For convenience, this library provides an easy way to import external objects from HTTPX and RESPX.
Function¶
For more complex scenarios or for taking advantage of RESPX side effects, you can also define the response as a function as long as that function returns an HTTPX response object.
The function's signature must match one of:
(request: httpx.Request) -> httpx.Response
(request: httpx.Request, route: respx.Route) -> httpx.Response
(request: httpx.Request, route: respx.Route, ,*, state: openai_responses.StateStore) -> httpx.Response
(request: httpx.Request, route: respx.Route, ,*, state: openai_responses.StateStore, ...) -> httpx.Response
Looking at a real-life example, this test simulates two failed calls before finally succeeding on the third call.
Note
This example also makes use of helpers which are convenient utilities for common operations.
State store injection¶
For functions used with stateful routes you can add state_store
as an argument or keyword-only argument and it will be automatically provided.
Path parameters¶
If a route has path parameters then those will also be automatically passed to the response function.
For example, the route for retrieving runs is:
For functions, you can access those path parameters like this:
Warning
If a route has path parameters but you do not need them in the function signature then you must add kwargs
to the function. These arguments are automatically added to the function and without them in the signature or without using kwargs
you will get an error.