Request metadata available to Python profile runtimes.
| 18 | |
| 19 | @dataclass(frozen=True, slots=True, init=False) |
| 20 | class ProfileRequestMetadata: |
| 21 | """Request metadata available to Python profile runtimes.""" |
| 22 | |
| 23 | request_id: str | None |
| 24 | inbound_format: ChatRequestType | None |
| 25 | headers: dict[str, list[str]] |
| 26 | |
| 27 | def __init__( |
| 28 | self, |
| 29 | request_id: str | None = None, |
| 30 | inbound_format: ChatRequestType | str | None = None, |
| 31 | headers: Mapping[str, str | list[str]] | None = None, |
| 32 | ) -> None: |
| 33 | if request_id is not None and not request_id.strip(): |
| 34 | raise ValueError("invalid request_id: RequestId must not be empty") |
| 35 | object.__setattr__(self, "request_id", request_id) |
| 36 | object.__setattr__( |
| 37 | self, |
| 38 | "inbound_format", |
| 39 | request_type_enum(inbound_format) if inbound_format is not None else None, |
| 40 | ) |
| 41 | object.__setattr__(self, "headers", _normalize_headers(headers)) |
| 42 | |
| 43 | @classmethod |
| 44 | def from_headers( |
| 45 | cls, |
| 46 | headers: Mapping[str, str | list[str]], |
| 47 | inbound_format: ChatRequestType | str | None = None, |
| 48 | ) -> ProfileRequestMetadata: |
| 49 | """Build metadata from headers and infer the request ID when present.""" |
| 50 | normalized = _normalize_headers(headers) |
| 51 | request_ids = normalized.get("x-request-id", []) |
| 52 | return cls( |
| 53 | request_id=request_ids[0] if request_ids else None, |
| 54 | inbound_format=inbound_format, |
| 55 | headers=normalized, |
| 56 | ) |
| 57 | |
| 58 | def to_dict(self) -> dict[str, object]: |
| 59 | """Return a plain dictionary representation.""" |
| 60 | return { |
| 61 | "request_id": self.request_id, |
| 62 | "inbound_format": ( |
| 63 | request_type_value(self.inbound_format) |
| 64 | if self.inbound_format is not None |
| 65 | else None |
| 66 | ), |
| 67 | "headers": {name: list(values) for name, values in self.headers.items()}, |
| 68 | } |
| 69 | |
| 70 | |
| 71 | @dataclass(frozen=True, slots=True, init=False) |
no outgoing calls