44 lines
2.3 KiB
Python
44 lines
2.3 KiB
Python
"""Allowlisted failure details: never response text, headers or provider errors."""
|
|
REASONS = {'stop','length','content_filter','tool_calls','function_call','MAX_TOKENS','SAFETY','STOP','OTHER','unknown','missing'}
|
|
COUNTS = {'input_tokens','output_tokens','reasoning_tokens','response_bytes','output_characters','max_output_tokens'}
|
|
KEYWORDS = {'type','required','additionalProperties','enum','const','pattern','minItems','maxItems','minimum','maximum','anyOf','oneOf'}
|
|
|
|
|
|
def count(value):
|
|
return value if type(value) is int and 0 <= value <= 10**12 else None
|
|
|
|
|
|
def sanitize(data):
|
|
if not isinstance(data,dict):
|
|
return {}
|
|
result = {k:count(v) for k,v in data.items() if k in COUNTS}
|
|
if 'finish_reason' in data:
|
|
value = data['finish_reason']
|
|
result['finish_reason'] = value if isinstance(value,str) and value in REASONS else ('missing' if value is None else 'unknown')
|
|
for key in ('has_refusal','has_tool_calls'):
|
|
if type(data.get(key)) is bool:
|
|
result[key] = data[key]
|
|
if isinstance(data.get('validation_keyword'),str) and data['validation_keyword'] in KEYWORDS:
|
|
result['validation_keyword'] = data['validation_keyword']
|
|
return result
|
|
|
|
|
|
def envelope_details(envelope, max_output_tokens, response_bytes):
|
|
result = {'max_output_tokens':max_output_tokens,'response_bytes':response_bytes}
|
|
if isinstance(envelope,dict):
|
|
usage = envelope.get('usage')
|
|
if isinstance(usage,dict):
|
|
result.update(input_tokens=usage.get('prompt_tokens'),output_tokens=usage.get('completion_tokens'))
|
|
details = usage.get('completion_tokens_details')
|
|
if isinstance(details,dict):
|
|
result['reasoning_tokens'] = details.get('reasoning_tokens')
|
|
choices = envelope.get('choices')
|
|
if isinstance(choices,list) and choices and isinstance(choices[0],dict):
|
|
result['finish_reason'] = choices[0].get('finish_reason')
|
|
message = choices[0].get('message')
|
|
if isinstance(message,dict):
|
|
result.update(has_refusal=bool(message.get('refusal')),has_tool_calls=bool(message.get('tool_calls')))
|
|
if isinstance(message.get('content'),str):
|
|
result['output_characters'] = len(message['content'])
|
|
return sanitize(result)
|