Api call return empty, but works in playground

Hi, any can tell me why this return empty text

{"prompt":"summarises: Quantity: Each package weighs around 500 g and the stone is approximately 160-200 pieces. Size: 8-12 mm, oval shape. Due to the different sizes and weights of the stone, the amount of stones can be a bit different due to their understanding.,Wide application: Decorative stones for aquariums can be used in aquariums, stone gardens, tilandsia plants, cactus pots, terrariums, bamboo plants and bonsai trees. You can add beauty to all your needs. In addition, you can also make a cover or shoes to massage the soles of the feet. This can relieve stress and get healthier.,Several colors: give your life a different visual impression and gives it vitality. Ideal decoration for plants, aquarium, landscaping, etc. It comes in a variety of natural colors, it is not discolored for lasting beauty. And the combination of popular natural patterns and tones such as brown, white, black, red, gray. Each piece can be a work of art.,Tips: rinse the stones before placing them in the aquarium. Keep away from children under 3 years to prevent stones from getting in their mouths. The rocks are natural and, therefore, some can be broken or deformed, oval or round.","temperature":0.7,"max_tokens":256,"top_p":1,"frequency_penalty":0,"presence_penalty":0}

I have made a script that works with curl and reads a csv and sends it to the api. And it returns the empty text.
But if I paste this same text with the same configuration and paste it in the playground it works.
This example works with text-babbage-001

Thanks!!!

Hi @srnatan

Welcome to the OpenAI community.

Sharing the complete script and API response will be very helpful.

  $engine = "text-babbage-001";
  $url = "https://api.openai.com/v1/engines/$engine/completions";
  $OPENAI_API_KEY = 'MY_API_KEY';
  $authorization = "Authorization: Bearer $OPENAI_API_KEY";

  $clean_text= str_replace(["\n","\r"],"",$tex);

  $data = array(  "prompt" => "$clean_text",
                  "temperature" => floatval($temperature),
                  "max_tokens"=> intval($len),
                  "top_p" => 1,
                  "frequency_penalty" => 0,
                  "presence_penalty" => 0
                );


  $postdata = json_encode($data);


  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));
  $result = curl_exec($ch);
  curl_close($ch);

And response is

{"id": "cmpl-54VANVDuu0zSBHRgYX4PA0Vaszc5T", "object": "text_completion", "created": 1651754799, "model": "text-babbage:001", "choices": [{"text": "", "index": 0, "logprobs": null, "finish_reason": "stop"}]}
1 Like

Try to append a whitespace end of the text. Probably the engine is already assuming it has reached the end of the text.

3 Likes

OMG Thank you. With a whitespace at the end, all requests return text. It was driving me crazy

4 Likes

I found this question searching for “empty JSON”.

I had a different solution: my prompt string contained a single-quote and I had to escape it.