从请求中提取 aspectRatio 和 imageSize 参数。 优先级: 1. request.generationConfig.imageConfig (顶层 Gemini 参数) 2. extra fields 中的 generationConfig (extra_body 透传) 3. OpenAI 风格字段(size/quality)兼容:可在 generationConfig/imageConfig 或顶层 extra 中出现 Returns: (aspect_ratio, image_size) 归一化后的值
(request)
| 404 | |
| 405 | |
| 406 | def _extract_generation_params(request) -> Tuple[Optional[str], Optional[str]]: |
| 407 | """从请求中提取 aspectRatio 和 imageSize 参数。 |
| 408 | |
| 409 | 优先级: |
| 410 | 1. request.generationConfig.imageConfig (顶层 Gemini 参数) |
| 411 | 2. extra fields 中的 generationConfig (extra_body 透传) |
| 412 | 3. OpenAI 风格字段(size/quality)兼容:可在 generationConfig/imageConfig 或顶层 extra 中出现 |
| 413 | |
| 414 | Returns: |
| 415 | (aspect_ratio, image_size) 归一化后的值 |
| 416 | """ |
| 417 | def _normalize_str(value: Any) -> Optional[str]: |
| 418 | if not isinstance(value, str): |
| 419 | return None |
| 420 | text = value.strip() |
| 421 | return text if text else None |
| 422 | |
| 423 | def _read_value(obj: Any, *keys: str) -> Any: |
| 424 | if obj is None: |
| 425 | return None |
| 426 | if isinstance(obj, dict): |
| 427 | for key in keys: |
| 428 | if key in obj: |
| 429 | return obj.get(key) |
| 430 | return None |
| 431 | |
| 432 | for key in keys: |
| 433 | if hasattr(obj, key): |
| 434 | value = getattr(obj, key, None) |
| 435 | if value is not None: |
| 436 | return value |
| 437 | |
| 438 | extra = getattr(obj, "__pydantic_extra__", None) or {} |
| 439 | for key in keys: |
| 440 | if key in extra: |
| 441 | return extra.get(key) |
| 442 | return None |
| 443 | |
| 444 | def _normalize_aspect_ratio(value: Any) -> Optional[str]: |
| 445 | raw = _normalize_str(value) |
| 446 | if not raw: |
| 447 | return None |
| 448 | |
| 449 | token = ( |
| 450 | raw.replace(":", ":") |
| 451 | .replace("/", ":") |
| 452 | .replace("x", ":") |
| 453 | .replace("X", ":") |
| 454 | .replace(" ", "") |
| 455 | .strip() |
| 456 | ) |
| 457 | |
| 458 | mapped = ASPECT_RATIO_MAP.get(token) |
| 459 | if mapped: |
| 460 | return mapped |
| 461 | mapped = ASPECT_RATIO_MAP.get(token.lower()) |
| 462 | if mapped: |
| 463 | return mapped |
no test coverage detected