responses_to_chatcompletions_tool_name_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package apicompat
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/stretchr/testify/require"
  6. )
  7. // TestResponsesEventToChatChunks_ArgumentsDeltaOmitsEmptyName pins that
  8. // streamed arguments-only deltas never re-emit the tool name as an empty
  9. // string. OpenAI-compatible clients accumulate the name from the first
  10. // tool-call delta; a trailing "name":"" would overwrite the accumulated name
  11. // and break tool dispatch ("unknown tool").
  12. func TestResponsesEventToChatChunks_ArgumentsDeltaOmitsEmptyName(t *testing.T) {
  13. state := NewResponsesEventToChatState()
  14. added := ResponsesEventToChatChunks(&ResponsesStreamEvent{
  15. Type: "response.output_item.added",
  16. OutputIndex: 0,
  17. Item: &ResponsesOutput{
  18. Type: "function_call",
  19. CallID: "call_a",
  20. Name: "exec",
  21. },
  22. }, state)
  23. require.Len(t, added, 1)
  24. first, err := json.Marshal(added[0])
  25. require.NoError(t, err)
  26. require.Contains(t, string(first), `"name":"exec"`)
  27. for _, fragment := range []string{`{"cmd":"ls"}`, `{"cmd":"ls","flags":"-la"}`} {
  28. deltas := ResponsesEventToChatChunks(&ResponsesStreamEvent{
  29. Type: "response.function_call_arguments.delta",
  30. OutputIndex: 0,
  31. Delta: fragment,
  32. }, state)
  33. require.Len(t, deltas, 1)
  34. raw, err := json.Marshal(deltas[0])
  35. require.NoError(t, err)
  36. require.NotContains(t, string(raw), `"name"`, "arguments delta must not carry a name field")
  37. require.Contains(t, string(raw), `"arguments"`)
  38. }
  39. }