@@ -592,3 +592,57 @@ func TestXAIExecutorExecuteVideosUsesNativeEndpointFromRequestPath(t *testing.T)
592592 })
593593 }
594594}
595+
596+ func TestNormalizeXAIToolChoiceForTools_DropsWhenToolsEmpty (t * testing.T ) {
597+ body := []byte (`{"model":"grok-4","tools":[],"tool_choice":"auto","parallel_tool_calls":true,"input":"hi"}` )
598+ out := normalizeXAIToolChoiceForTools (body )
599+
600+ if gjson .GetBytes (out , "tools" ).Exists () {
601+ t .Fatalf ("empty tools should be removed: %s" , string (out ))
602+ }
603+ if gjson .GetBytes (out , "tool_choice" ).Exists () {
604+ t .Fatalf ("tool_choice should be removed when tools empty: %s" , string (out ))
605+ }
606+ if gjson .GetBytes (out , "parallel_tool_calls" ).Exists () {
607+ t .Fatalf ("parallel_tool_calls should be removed when tools empty: %s" , string (out ))
608+ }
609+ }
610+
611+ func TestNormalizeXAIToolChoiceForTools_DropsWhenToolsMissing (t * testing.T ) {
612+ body := []byte (`{"model":"grok-4","tool_choice":"auto","input":"hi"}` )
613+ out := normalizeXAIToolChoiceForTools (body )
614+
615+ if gjson .GetBytes (out , "tool_choice" ).Exists () {
616+ t .Fatalf ("tool_choice should be removed when tools missing: %s" , string (out ))
617+ }
618+ }
619+
620+ func TestNormalizeXAIToolChoiceForTools_DropsOrphanedParallelToolCalls (t * testing.T ) {
621+ body := []byte (`{"model":"grok-4","parallel_tool_calls":true,"input":"hi"}` )
622+ out := normalizeXAIToolChoiceForTools (body )
623+
624+ if gjson .GetBytes (out , "parallel_tool_calls" ).Exists () {
625+ t .Fatalf ("parallel_tool_calls should be removed when tools missing even without tool_choice: %s" , string (out ))
626+ }
627+ }
628+
629+ func TestNormalizeXAIToolChoiceForTools_KeepsWhenToolsPresent (t * testing.T ) {
630+ body := []byte (`{"model":"grok-4","tools":[{"type":"function","name":"Bash"}],"tool_choice":"auto","input":"hi"}` )
631+ out := normalizeXAIToolChoiceForTools (body )
632+
633+ if ! gjson .GetBytes (out , "tools" ).Exists () {
634+ t .Fatalf ("tools should be kept: %s" , string (out ))
635+ }
636+ if got := gjson .GetBytes (out , "tool_choice" ).String (); got != "auto" {
637+ t .Fatalf ("tool_choice = %q, want auto: %s" , got , string (out ))
638+ }
639+ }
640+
641+ func TestNormalizeXAIToolChoiceForTools_NoOpWhenBothAbsent (t * testing.T ) {
642+ body := []byte (`{"model":"grok-4","input":"hi"}` )
643+ out := normalizeXAIToolChoiceForTools (body )
644+
645+ if gjson .GetBytes (out , "tool_choice" ).Exists () {
646+ t .Fatalf ("tool_choice should not appear: %s" , string (out ))
647+ }
648+ }
0 commit comments