fix: keep __annotations__ in wrap_plugin_route — body params need ForwardRef resolution

Removing __annotations__ broke body parameter resolution: FastAPI could
not resolve ForwardRef('ConversationCreate') etc. causing 422 on all
POST routes with body params. Now keeping annotations from functools.wraps
and only removing return_annotation.
This commit is contained in:
Agent Zero
2026-07-27 02:39:16 +02:00
parent d607803e86
commit 24fb384cf9
+7 -8
View File
@@ -3,7 +3,7 @@ import logging
import functools
import inspect
from fastapi import UploadFile # noqa: F401 — needed for ForwardRef resolution
from fastapi import WebSocket # noqa: F401 — needed for ForwardRef resolution of WebSocket params
from fastapi import WebSocket # noqa: F401 — needed for ForwardRef resolution
from fastapi.responses import JSONResponse
logger = logging.getLogger(__name__)
@@ -14,7 +14,8 @@ def wrap_plugin_route(handler):
Copies the original handler's signature so FastAPI sees the correct
parameters (path params, query params, body, etc.) instead of *args/**kwargs.
Removes return annotation to prevent ForwardRef('UploadFile') resolution issues.
UploadFile and WebSocket are imported in this module's namespace so
FastAPI can resolve ForwardRef('UploadFile') and ForwardRef('WebSocket').
"""
@functools.wraps(handler)
async def wrapper(*args, **kwargs):
@@ -26,18 +27,16 @@ def wrap_plugin_route(handler):
status_code=500,
content={'detail': f'Plugin error: {exc}', 'code': 'plugin_error'}
)
# Remove annotations that cause ForwardRef resolution issues
wrapper.__annotations__ = {}
# Remove __wrapped__ so FastAPI doesn't try to resolve the original signature
# through the wrapper chain — we set __signature__ explicitly instead.
if hasattr(wrapper, '__wrapped__'):
delattr(wrapper, '__wrapped__')
# Copy the signature from the original handler but remove return annotation
# Copy the signature from the original handler so FastAPI sees correct params.
# Keep __annotations__ from functools.wraps (needed for ForwardRef resolution).
# Remove only the return annotation to avoid response_model issues.
try:
orig_sig = inspect.signature(handler)
# Create new signature without return annotation
new_params = list(orig_sig.parameters.values())
wrapper.__signature__ = orig_sig.replace(
parameters=new_params,
return_annotation=inspect.Signature.empty,
)
except (ValueError, TypeError):