redis_openai_agents.middleware.SemanticCacheMiddleware#

class SemanticCacheMiddleware(cache, *, serializer=<function _default_serialize>, deserializer=<function _default_deserialize>, cacheable=None)[source]#

Cache LLM responses keyed by the semantic similarity of the input.

Guards against non-deterministic call contexts - requests are not cached when any of tools, handoffs, or output_schema are present, because the response typically depends on those side conditions and may not repeat.

Parameters:
  • cache (SemanticCache) – A SemanticCache instance managing the underlying two-level cache.

  • serializer (Serializer) – Callable that converts a model response to a string for storage. Defaults to pickle+base64.

  • deserializer (Deserializer) – Inverse of serializer.

  • cacheable (Callable[[ModelRequest], bool] | None) – Optional predicate that returns False to skip caching for a given request even if the default guards would allow it. Applied after the default guards.

__init__(cache, *, serializer=<function _default_serialize>, deserializer=<function _default_deserialize>, cacheable=None)[source]#
Parameters:
  • cache (SemanticCache)

  • serializer (Serializer)

  • deserializer (Deserializer)

  • cacheable (Callable[[ModelRequest], bool] | None)

Return type:

None

Methods

__init__(cache, *[, serializer, ...])

awrap_model_call(request, handler)