import unittest from api_compat import normalize_chat,CompatibilityError class CompatibilityTests(unittest.TestCase): def test_levels_and_alias_without_mutation(self): for value in ['none','minimal','low','medium','high','xhigh','max','ultra']: request={'model':'any','reasoning_effort':value};body,info=normalize_chat(request) self.assertEqual(request['reasoning_effort'],value) self.assertEqual(body['reasoning_effort'],{'minimal':'low','max':'xhigh','ultra':'xhigh'}.get(value,value)) self.assertEqual(info['requested'],value) def test_unset_and_null_do_not_invent_defaults(self): for request in [{},{'reasoning_effort':None}]: self.assertEqual(normalize_chat(request),({},{})) def test_invalid_inputs(self): for value in [True,1,{},[], '', 'MAX', 'x\r\ny']: with self.assertRaises(CompatibilityError):normalize_chat({'reasoning_effort':value}) with self.assertRaises(CompatibilityError):normalize_chat({'unknown':1}) def test_template_thinking_options(self): original={'chat_template_kwargs':{'enable_thinking':False,'preserve_thinking':True}} body,_=normalize_chat(original) self.assertEqual(body,original) body['chat_template_kwargs']['enable_thinking']=True self.assertFalse(original['chat_template_kwargs']['enable_thinking']) for bad in [None,[],{'enable_thinking':'false'},{'template':'custom'},{'preserve_thinking':1}]: with self.assertRaises(CompatibilityError):normalize_chat({'chat_template_kwargs':bad}) def test_openai_aliases_and_legacy_tools(self): request={'max_completion_tokens':32,'functions':[{'name':'echo','parameters':{'type':'object'}}],'function_call':{'name':'echo'},'stop':None} body,_=normalize_chat(request) self.assertEqual(body['max_tokens'],32) self.assertEqual(body['tools'][0]['function']['name'],'echo') self.assertEqual(body['tool_choice']['function']['name'],'echo') self.assertNotIn('stop',body) self.assertIn('functions',request) def test_sdk_extra_body_and_conflicts(self): body,_=normalize_chat({'extra_body':{'min_p':.05,'chat_template_kwargs':{'enable_thinking':False}},'max_tokens':8}) self.assertEqual(body['min_p'],.05) for request in [{'extra_body':{'model':'other'}},{'extra_body':{'min_p':.1},'min_p':.2},{'max_tokens':8,'max_completion_tokens':16},{'functions':[],'tools':[{}]}]: with self.assertRaises(CompatibilityError):normalize_chat(request) def test_metadata_is_explicitly_reported_not_forwarded(self): body,info=normalize_chat({'store':False,'metadata':{'project':'synthetic'},'service_tier':'default'}) self.assertEqual(body,{}) self.assertEqual(set(info['ignored']),{'store','metadata','service_tier'}) for request in [{'store':True},{'service_tier':'priority'},{'metadata':{'x':42}}]: with self.assertRaises(CompatibilityError):normalize_chat(request) def test_llama_validation(self): body,_=normalize_chat({'repeat_last_n':-1,'mirostat':2,'mirostat_eta':.1,'typical_p':1,'cache_prompt':True,'reasoning_format':'auto','logit_bias':{'123':-10}}) self.assertEqual(body['logit_bias'],{'123':-10}) for request in [{'min_p':float('nan')},{'cache_prompt':1},{'repeat_last_n':.5},{'logit_bias':{'not-token':1}},{'mirostat':3},{'reasoning_format':'invented'}]: with self.assertRaises(CompatibilityError):normalize_chat(request)