Repository navigation
129: Refactoring decorators - #188
JaeYeonLee0621 wants to merge 5 commits into
Conversation
| ) | ||
| from gateway.decorators.responses import check_tool_availability, validate_response_id | ||
|
|
||
| __all__ = [ |
There was a problem hiding this comment.
Should we use __all__ here? I guess we just assume we want to export everything and we still have private functions with _. This is always more work with little gain.
There was a problem hiding this comment.
Agreed, __all__ isn't needed here, so I've removed it. External code can import the decorators using their full path.
There was a problem hiding this comment.
We also use it in the model refactor so you can also leave it... sorry that was probably a bit premature.
| return wrapper | ||
|
|
||
|
|
||
| def check_limits(view_func: AsyncView) -> AsyncView: |
There was a problem hiding this comment.
I think check_limits should not belong to auth. As we already have plans to expand how we handle limits, this should get its own file I think.
There was a problem hiding this comment.
I put the check_limits definition in a new limits.py file. I'm open to suggestions on the file name or placement :)
| return False | ||
|
|
||
|
|
||
| def normalize_reasoning_fields(view_func: AsyncView) -> AsyncView: |
There was a problem hiding this comment.
Tangential but do we really only need to normalize reasoning fields for chat completions and not responses? I forgot but maybe the Responses was unified from the beginning anyways.
There was a problem hiding this comment.
I'm not sure I fully follow this comment. Could you explain it a bit more?
There was a problem hiding this comment.
There were inconsistencies with the different Chat Completions API implementations where some used reasoning and others reasoning_content so we just make sure both are in the response. I was wondering if the Responses API has the same issue.
There was a problem hiding this comment.
I'm not familiar with this part, but from what AI said, the Responses API path doesn't go through LiteLLM's routing. It calls client.responses.create() directly on an OpenAI-compatible SDK, and the result comes back as a canonical, strongly typed Response pydantic model. So It said, It doesn't have the same issue as the Chat Completions API.
There was a problem hiding this comment.
I do not really like the name of the file. Should we have a types.py file? Or leave it in utils.py? Basically can you check how many custom utility classes and functions we have/expect to have and split accordingly?
There was a problem hiding this comment.
I renamed the file to response_type.py, since putting all these classes in a utils.py file didn't feel right and types.py seemed too generic. Let me know if you'd prefer something else and I'll change it.
There was a problem hiding this comment.
Yes ok that makes sense! Also note that this file heavily depends on 152-http-requests-processing and I wanted to check if we could rewrite the classes so that we can get rid of the middleware introduced there. So we might have to merge this first otherwise we have to merge again here etc.
| return status_map.get(status, "invalid_request_error") | ||
|
|
||
|
|
||
| def in_wildcard(value: str | None, allowed_values: list[str]) -> bool: |
There was a problem hiding this comment.
This is probably a util function right?
There was a problem hiding this comment.
I moved this function to utils.py.
| return valid | ||
|
|
||
|
|
||
| def register_response_in_cache(response_id: str | None, model: str, email: str) -> None: |
There was a problem hiding this comment.
This and the following functions are specific to the Responses API and not related to Django HTTP responses at all so these should be moved.
There was a problem hiding this comment.
I moved the cache-related definitions into response_cache.py.
There was a problem hiding this comment.
I think they should be in views/responses.py since it is so specific to the Responses endpoint. But I am not sure.
There was a problem hiding this comment.
I tried making this change, but it runs into two circular imports.
Before
responses_cache.py (register/get/delete) → decorators/responses.py (validate_response_id)
responses_cache.py (register/get/delete) → views/responses.py (create_response)
responses_cache.py (register/get/delete) → views/utils.py (ResponseRegistrationWrapper)
After (functions moved to views/responses.py):
decorators/responses.py (validate_response_id) <-> views/responses.py (get_response_from_cache)
views/utils.py(ResponseRegistrationWrapper) <-> views/responses.py (register_response_in_cache)
1. Decorators refactored
Split the large
gateway/views/decorators.pyinto a newgateway/decorators/package with one module per concern:The old
gateway/views/decorators.pywas deleted.2. Review opinions applied
check_mcp_server_availabilitymoved intomcp.py.get_relay_model_nameremoved (it was unused).process_file_contentandnormalize_reasoning_fieldsgrouped inchat_completions.py.Utility functionsplaced next to the decorators that use them, rather than in a separate utils module.