mirror of
https://github.com/meta-llama/llama-stack.git
synced 2025-10-12 05:54:38 +00:00
refactor
# What does this PR do? ## Test Plan
This commit is contained in:
parent
548ccff368
commit
ab7888e927
32 changed files with 571 additions and 885 deletions
|
@ -184,7 +184,17 @@ async def lifespan(app: StackApp):
|
|||
|
||||
def is_streaming_request(func_name: str, request: Request, **kwargs):
|
||||
# TODO: pass the api method and punt it to the Protocol definition directly
|
||||
return kwargs.get("stream", False)
|
||||
# Check for stream parameter at top level (old API style)
|
||||
if "stream" in kwargs:
|
||||
return kwargs["stream"]
|
||||
|
||||
# Check for stream parameter inside Pydantic request params (new API style)
|
||||
if "params" in kwargs:
|
||||
params = kwargs["params"]
|
||||
if hasattr(params, "stream"):
|
||||
return params.stream
|
||||
|
||||
return False
|
||||
|
||||
|
||||
async def maybe_await(value):
|
||||
|
@ -289,21 +299,34 @@ def create_dynamic_typed_route(func: Any, method: str, route: str) -> Callable:
|
|||
if method == "post":
|
||||
# Annotate parameters that are in the path with Path(...) and others with Body(...),
|
||||
# but preserve existing File() and Form() annotations for multipart form data
|
||||
new_params = (
|
||||
[new_params[0]]
|
||||
+ [
|
||||
(
|
||||
def should_embed(param: inspect.Parameter) -> bool:
|
||||
"""Determine if Body should use embed=True or embed=False.
|
||||
|
||||
For OpenAI-compatible endpoints (param name is 'params'), use embed=False
|
||||
so the request body is parsed directly as the model (not nested).
|
||||
This allows OpenAI clients to send standard OpenAI format.
|
||||
For other endpoints, use embed=True for SDK compatibility.
|
||||
"""
|
||||
param_type = param.annotation
|
||||
# HACK
|
||||
# Check if it's a Pydantic BaseModel and param name is 'params' (OpenAI-compatible)
|
||||
if inspect.isclass(param_type) and issubclass(param_type, BaseModel) and param.name == "params":
|
||||
return False
|
||||
return True
|
||||
|
||||
original_params = new_params[1:] # Skip request parameter
|
||||
new_params = [new_params[0]] # Keep request parameter
|
||||
|
||||
for param in original_params:
|
||||
if param.name in path_params:
|
||||
new_params.append(
|
||||
param.replace(annotation=Annotated[param.annotation, FastapiPath(..., title=param.name)])
|
||||
if param.name in path_params
|
||||
else (
|
||||
param # Keep original annotation if it's already an Annotated type
|
||||
if get_origin(param.annotation) is Annotated
|
||||
else param.replace(annotation=Annotated[param.annotation, Body(..., embed=True)])
|
||||
)
|
||||
)
|
||||
for param in new_params[1:]
|
||||
]
|
||||
)
|
||||
elif get_origin(param.annotation) is Annotated:
|
||||
new_params.append(param) # Keep existing annotation
|
||||
else:
|
||||
embed = should_embed(param)
|
||||
new_params.append(param.replace(annotation=Annotated[param.annotation, Body(..., embed=embed)]))
|
||||
|
||||
route_handler.__signature__ = sig.replace(parameters=new_params)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue