MCPcopy Create free account
hub / github.com/TheSmallHanCat/flow2api / stream_generate_content

Function stream_generate_content

src/api/routes.py:943–976  ·  view source on GitHub ↗

Gemini official streamGenerateContent endpoint.

(
    model: str,
    request: GeminiGenerateContentRequest,
    raw_request: Request,
    alt: Optional[str] = Query(None),
    api_key: str = Depends(verify_api_key_flexible),
)

Source from the content-addressed store, hash-verified

941@router.post("/v1beta/models/{model}:streamGenerateContent")
942@router.post("/models/{model}:streamGenerateContent")
943async def stream_generate_content(
944 model: str,
945 request: GeminiGenerateContentRequest,
946 raw_request: Request,
947 alt: Optional[str] = Query(None),
948 api_key: str = Depends(verify_api_key_flexible),
949):
950 """Gemini official streamGenerateContent endpoint."""
951 try:
952 normalized = await _normalize_gemini_request(model, request)
953 if not normalized.prompt:
954 raise HTTPException(status_code=400, detail="Prompt cannot be empty")
955
956 request_base_url = _get_request_base_url(raw_request)
957
958 return StreamingResponse(
959 _iterate_gemini_stream(normalized, normalized.model, request_base_url),
960 media_type="text/event-stream",
961 headers={
962 "Cache-Control": "no-cache",
963 "Connection": "keep-alive",
964 "X-Accel-Buffering": "no",
965 },
966 )
967 except HTTPException as exc:
968 return JSONResponse(
969 status_code=exc.status_code,
970 content=_build_gemini_error_payload(exc.status_code, str(exc.detail)),
971 )
972 except Exception as exc:
973 return JSONResponse(
974 status_code=500,
975 content=_build_gemini_error_payload(500, str(exc)),
976 )
977
978@router.websocket("/captcha_ws")
979async def captcha_websocket_endpoint(websocket: WebSocket):

Callers

nothing calls this directly

Calls 4

_get_request_base_urlFunction · 0.85
_iterate_gemini_streamFunction · 0.85

Tested by

no test coverage detected