@@ -305,12 +305,30 @@ def _remove_inherited_session_auth(
305305 if profile_name == "DEFAULT" or "security_token_file" not in oci_config :
306306 return
307307
308+ config_file = os .path .expanduser (config_path or "~/.oci/config" )
308309 parser = configparser .ConfigParser (interpolation = None )
309- if not parser .read (os . path . expanduser ( config_path or "~/.oci/config" ) ):
310+ if not parser .read (config_file ):
310311 return
311312
312- explicit_profile = parser ._sections .get (profile_name , {})
313- if "security_token_file" not in explicit_profile :
313+ if not parser .has_section (profile_name ):
314+ oci_config .pop ("security_token_file" , None )
315+ return
316+
317+ explicit_security_token = False
318+ current_section : typing .Optional [str ] = None
319+ with open (config_file , encoding = "utf-8" ) as handle :
320+ for raw_line in handle :
321+ line = raw_line .strip ()
322+ if not line or line .startswith (("#" , ";" )):
323+ continue
324+ if line .startswith ("[" ) and line .endswith ("]" ):
325+ current_section = line [1 :- 1 ].strip ()
326+ continue
327+ if current_section == profile_name and line .split ("=" , 1 )[0 ].strip () == "security_token_file" :
328+ explicit_security_token = True
329+ break
330+
331+ if not explicit_security_token :
314332 oci_config .pop ("security_token_file" , None )
315333
316334
@@ -469,7 +487,6 @@ def _event_hook(request: httpx.Request) -> None:
469487 request .stream = ByteStream (oci_body_bytes )
470488 request ._content = oci_body_bytes
471489 request .extensions ["endpoint" ] = endpoint
472- request .extensions ["cohere_body" ] = body
473490 request .extensions ["is_stream" ] = "stream" in endpoint or body .get ("stream" , False )
474491 # Store V2 detection for streaming event transformation
475492 # For chat, detect V2 by presence of "messages" field (V2) vs "message" field (V1)
@@ -853,20 +870,11 @@ def transform_oci_response_to_cohere(
853870 }
854871
855872 # Add usage info if available
856- if "usage" in oci_response and oci_response ["usage" ]:
857- usage = oci_response ["usage" ]
858- # OCI usage has inputTokens, outputTokens, totalTokens
859- input_tokens = usage .get ("inputTokens" , 0 )
860- output_tokens = usage .get ("outputTokens" , 0 )
861-
862- meta ["billed_units" ] = {
863- "input_tokens" : input_tokens ,
864- "output_tokens" : output_tokens ,
865- }
866- meta ["tokens" ] = {
867- "input_tokens" : input_tokens ,
868- "output_tokens" : output_tokens ,
869- }
873+ usage = _usage_from_oci (oci_response .get ("usage" ))
874+ if "tokens" in usage :
875+ meta ["tokens" ] = usage ["tokens" ]
876+ if "billed_units" in usage :
877+ meta ["billed_units" ] = usage ["billed_units" ]
870878
871879 return {
872880 "id" : oci_response .get ("id" , str (uuid .uuid4 ())),
@@ -916,19 +924,11 @@ def transform_oci_response_to_cohere(
916924 "api_version" : {"version" : "1" },
917925 }
918926
919- if "usage" in chat_response and chat_response ["usage" ]:
920- usage = chat_response ["usage" ]
921- input_tokens = usage .get ("inputTokens" , 0 )
922- output_tokens = usage .get ("outputTokens" , 0 )
923-
924- meta ["billed_units" ] = {
925- "input_tokens" : input_tokens ,
926- "output_tokens" : output_tokens ,
927- }
928- meta ["tokens" ] = {
929- "input_tokens" : input_tokens ,
930- "output_tokens" : output_tokens ,
931- }
927+ usage = _usage_from_oci (chat_response .get ("usage" ))
928+ if "tokens" in usage :
929+ meta ["tokens" ] = usage ["tokens" ]
930+ if "billed_units" in usage :
931+ meta ["billed_units" ] = usage ["billed_units" ]
932932
933933 return {
934934 "text" : chat_response .get ("text" , "" ),
@@ -1056,16 +1056,17 @@ def _process_line(line: str) -> typing.Iterator[bytes]:
10561056 data_str = line [6 :]
10571057 if data_str .strip () == "[DONE]" :
10581058 if is_v2 :
1059- if emitted_start and not emitted_content_end :
1060- yield _emit_v2_event ({"type" : "content-end" , "index" : 0 })
1061- message_end_event : typing .Dict [str , typing .Any ] = {
1062- "type" : "message-end" ,
1063- "id" : generation_id ,
1064- "delta" : {"finish_reason" : final_finish_reason },
1065- }
1066- if final_usage :
1067- message_end_event ["delta" ]["usage" ] = final_usage
1068- yield _emit_v2_event (message_end_event )
1059+ if emitted_start :
1060+ if not emitted_content_end :
1061+ yield _emit_v2_event ({"type" : "content-end" , "index" : 0 })
1062+ message_end_event : typing .Dict [str , typing .Any ] = {
1063+ "type" : "message-end" ,
1064+ "id" : generation_id ,
1065+ "delta" : {"finish_reason" : final_finish_reason },
1066+ }
1067+ if final_usage :
1068+ message_end_event ["delta" ]["usage" ] = final_usage
1069+ yield _emit_v2_event (message_end_event )
10691070 else :
10701071 yield _emit_v1_event (
10711072 {
0 commit comments