Ë
    tPêj’D  ã                  óp  — d Z ddlmZ ddlmZmZ ddlmZmZ ddl	m
Z
 ddlmZmZmZmZ ddlmZ ddlmZ dd	lmZmZ erdd
lmZ  ed¬«       G d„ d«      «       Z ed¬«       G d„ d«      «       Z ed¬«       G d„ d«      «       Zdd„Zdd„Zd d„Zd!d„Z	 	 	 	 	 	 d"d„Z d#d„Z!d$d„Z"d%d„Z#d&d„Z$d'd„Z%y)(uÓ  The single translation layer between a request's metadata and the spans.

Every relevant field litellm exposes about a request â€” the user-facing model,
the model actually dispatched to the provider, the deployment, and the caller's
identity (team, key, end-user) â€” is parsed **once**, here, out of the
``StandardLoggingPayload`` (or a ``UserAPIKeyAuth`` at the auth boundary). Span
data, baggage promotion, and the mappers then read these typed fields instead of
each digging into the raw ``metadata`` / ``hidden_params`` dicts.

Two models live here because a request's identity is known *before* its model
resolution is:

* :class:`RequestIdentity` â€” team / key / end-user, seeded into Baggage at the
  auth boundary (``from_user_api_key_auth``), before routing has picked a
  deployment. ``provider_model`` is therefore absent from that early seed and is
  only filled in from the payload once the call closes.
* :class:`RequestContext` â€” the full picture available at close: the resolved
  request vs. provider model split, plus the response model, model group, model
  id, and api base, wrapping the :class:`RequestIdentity`.

The request-vs-provider model split is the subtle part. On the proxy a caller
asks for a *model group* (e.g. ``gpt-4o``) that routes to a concrete deployment
(e.g. ``azure/my-deployment``); the two are distinct and both worth recording.
``StandardLoggingPayload`` exposes them as:

* ``model_group`` â€” the user-facing name the caller requested.
* ``model`` â€” already reconstructed (see ``reconstruct_model_name``) to the name
  litellm dispatched to the provider (the deployment, provider-prefixed).
* ``hidden_params.litellm_model_name`` â€” a secondary source for the dispatched
  model (populated only on some call paths, e.g. files).

So ``gen_ai.request.model`` is the *group* (falling back to the call model on the
SDK path, which has no group), and ``litellm.provider.model`` is the *dispatched*
model. They coincide on the SDK path, which is correct.
é    )Úannotations)ÚIteratorÚMapping)Ú	dataclassÚfield)ÚMappingProxyType)ÚTYPE_CHECKINGÚAnyÚFinalÚcast)Ú$LITELLM_LOGGING_NO_UPSTREAM_LLM_CALL)Úresolve_operation)Úas_strÚ
to_seconds)ÚStandardLoggingPayloadT)Úfrozenc                  ó°   — e Zd ZU dZded<   dZded<   dZded<   dZded<   dZded<   dZ	ded	<   dZ
ded
<    ee¬«      Zded<   edd„«       Zedd„«       Zy)ÚRequestIdentityNú
str | NoneÚcall_idÚteam_idÚ
team_aliasúMapping[str, Any] | NoneÚteam_metadataÚkey_hashÚend_userÚprovider_model)Údefault_factoryzMapping[str, str]Úmetadatac                ó:  — t        t        t        t        f   |j	                  d«      xs i «      }|j                  «       D ��ci c]2  \  }}t        |t        t        t        t        f«      sŒ'|t        |«      “Œ4 }}} | t        |j	                  d«      «      xs t        |j	                  d«      «      t        |j	                  d«      «      xs t        |j	                  d«      «      t        |j	                  d«      «      xs t        |j	                  d«      «      t        |j	                  d«      «      t        |j	                  d	«      «      t        |j	                  d
«      «      xs t        |j	                  d«      «      t        |«      |¬«      S c c}}w )a   Parse caller identity out of a closed request's payload metadata.

        ``provider_model`` is resolved here too (see :func:`resolve_provider_model`)
        so the identity carried into Baggage labels every span with the dispatched
        model, not just the user-facing one.
        r   Úlitellm_call_idÚidÚuser_api_key_team_idr   Úuser_api_key_team_aliasr   Úuser_api_key_team_metadataÚuser_api_key_hashr   Úuser_api_key_end_user_id)r   r   r   r   r   r   r   r   )r   r   ÚstrÚobjectÚgetÚitemsÚ
isinstanceÚboolÚintÚfloatr   Ú_team_metadata_dictÚresolve_provider_model)ÚclsÚpayloadÚraw_metaÚkeyÚvaluer   s         úm/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/integrations/otel/model/metadata.pyÚfrom_payloadzRequestIdentity.from_payloadE   sC  € ô œw¤s¬F {Ñ3°W·[±[ÀÓ5LÒ5RÐPRÓSˆØ6>·n±nÓ6F×u©
¨¨UÌ*ÐUZÔ]`ÔbfÔhkÔmrÐ\sÕJt�Cœ˜U›‘OÐuˆÑuÙÜ˜7Ÿ;™;Ð'8Ó9Ó:ÒW¼fÀWÇ[Á[ÐQUÓEVÓ>Wô ˜8Ÿ<™<Ð(>Ó?Ó@ÒcÄFÈ8Ï<É<ÐXaÓKbÓDcÜ˜hŸl™lÐ+DÓEÓFÒlÌ&ÐQY×Q]ÑQ]Ð^jÓQkÓJlÜ-¨h¯l©lÐ;WÓ.XÓYÜ˜HŸL™LÐ)<Ó=Ó>Ü˜GŸK™K¨
Ó3Ó4Òh¼¸x¿|¹|ÐLfÓ?gÓ8hÜ1°'Ó:Øô
ð 	
ùó vs   Á(FÁ-Fc                ó  ‡— ˆfd„}dD ��ci c]  \  }} ||«      x}r|t        |«      “Œ }}} | t         |d«      «      t         |d«      «      t         |d«      «      t         |d«      «      t         |d«      «      |¬«      S c c}}w )	uº  Identity from a ``UserAPIKeyAuth`` (duck-typed to keep this module
        free of a proxy import).

        Used in the pre-call hook to seed Baggage early â€” before any LLM,
        guardrail, or service span is created â€” so the whole request's spans
        inherit identity, not just the LLM-call span. Metadata sub-keys use the
        ``user_api_key_*`` names that ``baggage.DEFAULT_BAGGAGE_METADATA_KEYS``
        promotes.
        c                ó   •— t        ‰| d «      S ©N)Úgetattr)ÚnameÚauths    €r7   ú<lambda>z8RequestIdentity.from_user_api_key_auth.<locals>.<lambda>h   s   ø€ ¤'¨$°°dÓ";€ ó    ))Úuser_api_key_user_idÚuser_id)Úuser_api_key_org_idÚorg_id)Úuser_api_key_aliasÚ	key_alias)r'   Úend_user_idr   r   r   Úapi_keyrG   )r   r   r   r   r   r   )r(   r   r0   )r2   r>   r*   Úmeta_keyÚattrr6   r   s    `     r7   Úfrom_user_api_key_authz&RequestIdentity.from_user_api_key_auth]   sŸ   ø€ ó <ˆð#÷	
á�˜$ñ ˜T›Ð"�Ð"ð ”c˜%“jÑ ð	
ˆñ 	
ñ Ü™3˜y›>Ó*Ü™c ,Ó/Ó0Ü-©c°/Ó.BÓCÜ™C 	›NÓ+Ü™C Ó.Ó/ð ô	
ð 		
ùó	
s   Œ B)r3   r   Úreturnr   )r>   r)   rL   r   )Ú__name__Ú
__module__Ú__qualname__r   Ú__annotations__r   r   r   r   r   r   r   Údictr   Úclassmethodr8   rK   © r@   r7   r   r   4   sƒ   … à€GˆZÓØ€GˆZÓØ!€J�
Ó!ð /3€MÐ+Ó2Ø€HˆjÓØ€HˆjÓð "&€N�JÓ%Ù"'¸Ô"=€HÐÓ=àò
ó ð
ð. ò
ó ñ
r@   r   c                  ór   — e Zd ZU dZded<   ded<   ded<   ded<   ded<   d	ed
<   edd„«       Zedd„«       Zy)ÚRequestContexta@  The fully-resolved view of a closed request, parsed once from the payload.

    ``request_model`` is the user-facing requested model and ``provider_model``
    (on :attr:`identity`) is the model litellm dispatched to the provider; the two
    differ on the proxy (group vs. deployment) and coincide on the SDK path.
    r(   Úrequest_modelr   Úresponse_modelÚmodel_groupÚmodel_idÚapi_baser   Úidentityc                ó.   — | j                   j                  S )z?The dispatched-model name, carried on the identity for Baggage.)r[   r   )Úselfs    r7   r   zRequestContext.provider_model�   s   € ð �}‰}×+Ñ+Ð+r@   c                ó0  — t        t        t        t        f   |j	                  d«      xs i «      }t        t        t        t        f   |j	                  d«      xs i «      }|j	                  d«      }t        t        t        t        f   t        |t        «      r|ni «      }t        |j	                  d«      «      xs t        |j	                  d«      «      } | |xs t        |j	                  d«      «      xs dt        |j	                  d«      «      |t        |j	                  d«      «      xs t        |j	                  d«      «      t        |j	                  d	«      «      xs t        |j	                  d	«      «      t        j                  |«      ¬
«      S )Nr   Úhidden_paramsÚresponserX   ÚmodelÚ rY   Ú
model_inforZ   )rV   rW   rX   rY   rZ   r[   )r   r   r(   r)   r*   r,   rQ   r   Ú_model_info_idr   r8   )r2   r3   r4   ÚhiddenÚraw_responser`   rX   s          r7   Úfrom_standard_logging_payloadz,RequestContext.from_standard_logging_payload”   s7  € äœw¤s¬F {Ñ3°W·[±[ÀÓ5LÒ5RÐPRÓSˆÜœW¤S¬& [Ñ1°7·;±;¸Ó3OÒ3UÐSUÓVˆØ%Ÿk™k¨*Ó5ˆÜœw¤s¬F {Ñ3ÄZÐP\Ô^bÔEc±\ÐikÓlˆÜ# G§K¡K°Ó$>Ó?ÒfÄ6È(Ï,É,ÐWdÓJeÓCfˆÙð &ÒK¬°·±¸GÓ0DÓ)EÒKÈÜ! (§,¡,¨wÓ"7Ó8Ø#Ü˜GŸK™K¨
Ó3Ó4Òb¼ÀxÇ|Á|ÐT`ÓGaÓ8bÜ˜GŸK™K¨
Ó3Ó4ÒV¼¸v¿z¹zÈ*Ó?UÓ8VÜ$×1Ñ1°'Ó:ô

ð 
	
r@   N)rL   r   )r3   r   rL   rU   )	rM   rN   rO   Ú__doc__rP   Úpropertyr   rR   rg   rS   r@   r7   rU   rU      sS   … ñð ÓØÓØÓØÓØÓØÓàò,ó ð,ð ò
ó ñ
r@   rU   c                  ót   — e Zd ZU dZded<   ded<   ded<   ded	<   d
ed<   d
ed<   ded<   ded<   edd„«       Zy)ÚLLMCallEventaA  The typed view of the live callback ``kwargs`` (``model_call_details``).

    litellm hands every callback an untyped ``kwargs`` god object. The fields the
    OTel logger needs out of it are parsed **once**, here, so the ``CustomLogger``
    reads typed attributes instead of digging into the dict at each boundary.
    r   r   úStandardLoggingPayload | Noner3   r
   Údynamic_paramsúMapping[str, str] | NoneÚauth_metadatar-   Úis_no_upstream_callÚupstream_startedr(   Úprovisional_span_nameúfloat | NoneÚtime_to_first_chunk_secondsc                óÒ  — |j                  d«      }|rt        d|«      nd }t        t        |j                  d«      «      «      }t        |j                  d«      «      xs d} | t	        ||«      ||j                  d«      t        ||«      t        |j                  t        «      «      |j                  d«      d u|j                  › d|› �j                  «       t        |«      ¬	«      S )
NÚstandard_logging_objectr   Ú	call_typera   rb   Ú standard_callback_dynamic_paramsÚapi_call_start_timeú )r   r3   rm   ro   rp   rq   rr   rt   )r*   r   r   r   Ú_call_idro   r-   r   r6   Ústriprt   )r2   ÚkwargsÚraw_payloadr3   Ú	operationra   s         r7   Ú	from_dictzLLMCallEvent.from_dictÙ   sÌ   € à#ŸZ™ZÐ(AÓBˆÙHSœÐ6¸ÔDÐY]ˆÜ,¬V°F·J±J¸{Ó4KÓ-LÓMˆ	Ü˜fŸj™j¨Ó1Ó2Ò8°bˆÙÜ˜W fÓ-ØØ!Ÿ:™:Ð&HÓIÜ'¨°Ó8Ü $ V§Z¡ZÔ0TÓ%UÓ VØ#ŸZ™ZÐ(=Ó>ÀdÐJØ%.§_¡_Ð$5°Q°u°gÐ">×"DÑ"DÓ"FÜ(CÀFÓ(Kô	
ð 		
r@   N)r}   úMapping[str, Any]rL   rk   )rM   rN   rO   rh   rP   rR   r€   rS   r@   r7   rk   rk   ±   sW   … ñð Óð +Ó*ð Óð ,Ó+ð Óð
 Óð ÓØ!-Ó-àò
ó ñ
r@   rk   c                ó  — t        t        t        t        f   | j	                  d«      xs i «      }|j	                  d«      syt        | j	                  d«      «      }t        | j	                  d«      «      }|�|€y||z
  S )a  Seconds from the upstream request being issued (``api_call_start_time``)
    to the first streamed chunk (``completion_start_time``); ``None`` for
    non-streaming calls, where ``completion_start_time`` is backfilled with the
    end time and would not measure first-chunk latency.Úoptional_paramsÚstreamNry   Úcompletion_start_time)r   r   r(   r
   r*   r   )r}   rƒ   Úapi_call_startÚcompletion_starts       r7   rt   rt   ë   s}   € ô
 "¤'¬#¬s¨(Ñ"3°V·Z±ZÐ@QÓ5RÒ5XÐVXÓY€OØ×Ñ˜xÔ(ØÜ& v§z¡zÐ2GÓ'HÓI€NÜ(¨¯©Ð4KÓ)LÓMÐØÐÐ!1Ð!9ØØ˜nÑ,Ð,r@   c                óB   ‡— t        ˆfd„t        | |«      D «       d«      S )u·  The key/team config the proxy resolved at auth, or ``None`` off the proxy.

    Read from the payload once the call closes and from ``litellm_params`` at
    ``pre_call``, where no payload exists yet â€” the LLM-call span is *created* at
    ``pre_call``, so the tracer (and therefore the destination) must be
    resolvable there. Values arrive untyped, so non-string entries are dropped
    rather than passed on to header builders.
    c              3  óZ   •K  — | ]"  }t        |j                  d «      «      xŠr‰–— Œ$ y­w)Úuser_api_key_auth_metadataN)Ú_string_entriesr*   )Ú.0r   Útypeds     €r7   ú	<genexpr>z auth_metadata.<locals>.<genexpr>  s3   øè ø€ ò 	
àÜ(¨¯©Ð6RÓ)SÓTÐT�ÐTô ñ	
ùó   ƒ(+N)ÚnextÚ_metadata_dicts)r3   r}   r�   s     @r7   ro   ro   ú   s+   ø€ ô ó	
ä+¨G°VÓ<ô	
ð
 	óð r@   c                ó<   — t        | t        «      syt        d| «      S )zBA read-only view of ``value`` when it is a mapping, else ``None``.NúMapping[str, object])r,   r   r   ©r6   s    r7   Ú_as_str_mappingr•     s   € ä�eœWÔ%ØÜÐ&¨Ó.Ð.r@   c           
     ó²   — t        | «      }|€y t        |j                  «       D ��ci c]  \  }}t        |t        «      sŒ||“Œ c}}«      }|xs d S c c}}w r;   )r•   r   r+   r,   r(   )r6   Úentriesr5   Úitemr�   s        r7   r‹   r‹     sS   € Ü$ UÓ+€GØ€ØÜ#ÀÇÁÃ×$i±9°3¸ÔS]Ð^bÔdgÕSh S¨$¡YÓ$iÓj€EØŠ=�DÐùó %js   §A
Á A
c              #  óÐ   ‡‡K  — t        | «      }|� t        |j                  d«      «      }|�|–— t        |j                  d«      «      Š‰€yˆˆfd„dD «       E d{  –—†  y7 Œ­w)a  Request metadata dicts, closed-call payload first then the live kwargs.

    ``litellm_metadata`` is the metadata field on the Anthropic-shaped routes;
    litellm copies it onto ``metadata``, but both are yielded so a route that
    populates only one is still covered.
    Nr   Úlitellm_paramsc              3  óZ   •K  — | ]"  }t        ‰j                  |«      «      xŠ�‰–— Œ$ y ­wr;   ©r•   r*   )rŒ   r5   r   Úparamss     €€r7   rŽ   z"_metadata_dicts.<locals>.<genexpr>-  s1   øè ø€ ò àÜ'¨¯
©
°3«Ó8Ð8ˆHÐEô 	ñùr�   )r   Úlitellm_metadatarœ   )r3   r}   Úpayload_viewÚpayload_metadatar   r�   s       @@r7   r‘   r‘     sn   ùè ø€ ô *¨'Ó2€LØÐÜ"1°,×2BÑ2BÀ:Ó2NÓ"OÐØÐ'Ø"Ò"Ü# F§J¡JÐ/?Ó$@ÓA€FØ€~Øôà3ô÷ ò ús   „AA&ÁA$ÁA&c                ó®   — | �:t        | j                  d«      «      xs t        | j                  d«      «      }|r|S t        |j                  d«      «      S )zLThe call id from the payload (when closed) or the bare kwargs (at pre_call).r!   r"   )r   r*   )r3   r}   r   s      r7   r{   r{   4  sM   € àÐÜ §¡Ð,=Ó >Ó?Ò\Ä6È'Ï+É+ÐVZÓJ[ÓC\ˆÙØˆNÜ�&—*‘*Ð.Ó/Ó0Ð0r@   c                óX   — t        | t        «      rt        | j                  d«      «      S y)zöThe user-facing ``model`` from a pre-call ``data`` dict (``None`` if absent).

    Read at the auth boundary to label early Baggage before routing has resolved
    a deployment; ``data`` is duck-typed since it arrives untyped from the proxy.
    ra   N©r,   r   r   r*   )Údatas    r7   Úmodel_from_request_datar¥   =  s%   € ô �$œÔ Ü�d—h‘h˜wÓ'Ó(Ð(Ør@   c                óf  — t        t        t        t        f   | j	                  d«      xs i «      }t        t        t        t        f   | j	                  d«      xs i «      }t        |j	                  d«      «      xs6 t        |j	                  d«      «      xs t        | j	                  d«      «      S )uo  The model litellm dispatched to the provider, from the payload.

    Prefers the explicit ``hidden_params.litellm_model_name`` (set on call paths
    that know it, e.g. files), then the top-level ``model`` â€” which
    ``reconstruct_model_name`` has already resolved to the deployment's
    provider-prefixed name. Returns ``None`` only when neither is present.
    r   r_   Ú
deploymentÚlitellm_model_namera   )r   r   r(   r)   r*   r   )r3   r4   re   s      r7   r1   r1   H  sŽ   € ô œ7¤3¬ ;Ñ/°·±¸ZÓ1HÒ1NÈBÓO€HÜœ¤¤f Ñ-¨w¯{©{¸?Ó/KÒ/QÈrÓR€Fô 	ˆx�|‰|˜LÓ)Ó*Òv¬f°V·Z±ZÐ@TÓ5UÓ.VÒvÔZ`Ðah×alÑalÐmtÓauÓZvðr@   c                óX   — t        | t        «      rt        | j                  d«      «      S y)zFThe deployment id from a ``metadata.model_info`` sub-dict, if present.r"   Nr£   )rc   s    r7   rd   rd   Y  s#   € ä�*œgÔ&Ü�j—n‘n TÓ*Ó+Ð+Ør@   c                ó>   — t        | t        «      r| rt        | «      S y)a0  The team's free-form metadata as a raw mapping, or ``None`` when missing
    or empty.

    Carried raw on the identity and filtered to an operator allowlist only at
    Baggage-promotion time (see ``baggage.promoted_baggage``), so an empty case
    is dropped rather than carrying a useless ``{}``.
    N)r,   r   rQ   r”   s    r7   r0   r0   `  s   € ô �%œÔ!¡eÜ�E‹{ÐØr@   N)r}   r�   rL   rs   )r3   rl   r}   r“   rL   rn   )r6   r)   rL   zMapping[str, object] | None)r6   r)   rL   rn   )r3   rl   r}   r“   rL   zIterator[Mapping[str, object]])r3   rl   r}   r�   rL   r   )r¤   r)   rL   r   )r3   r   rL   r   )rc   r)   rL   r   )r6   r)   rL   r   )&rh   Ú
__future__r   Úcollections.abcr   r   Údataclassesr   r   Útypesr   Útypingr	   r
   r   r   Úlitellm.constantsr   Ú'litellm.integrations.otel.model.semconvr   Ú%litellm.integrations.otel.model.utilsr   r   Úlitellm.types.utilsr   r   rU   rk   rt   ro   r•   r‹   r‘   r{   r¥   r1   rd   r0   rS   r@   r7   ú<module>r´      sØ   ðñ"õH #ç -ß (Ý "ß 2Ó 2å BÝ Eß DáÝ:ñ �$Ô÷G
ð G
ó ðG
ñT �$Ô÷%
ð %
ó ð%
ñb �$Ô÷6
ð 6
ó ð6
ór-óó&/óðØ*ðØ4Hðà#óó01óóó"ô
r@   