| 842 | |
| 843 | |
| 844 | def add_request_id(obj: BaseModel, request_id: str | None) -> None: |
| 845 | obj._request_id = request_id |
| 846 | |
| 847 | # in Pydantic v1, using setattr like we do above causes the attribute |
| 848 | # to be included when serializing the model which we don't want in this |
| 849 | # case so we need to explicitly exclude it |
| 850 | if PYDANTIC_V1: |
| 851 | try: |
| 852 | exclude_fields = obj.__exclude_fields__ # type: ignore |
| 853 | except AttributeError: |
| 854 | cast(Any, obj).__exclude_fields__ = {"_request_id", "__exclude_fields__"} |
| 855 | else: |
| 856 | cast(Any, obj).__exclude_fields__ = {*(exclude_fields or {}), "_request_id", "__exclude_fields__"} |
| 857 | |
| 858 | |
| 859 | # our use of subclassing here causes weirdness for type checkers, |