#πŸ”’ python api not sending full data.

30 messages Β· Page 1 of 1 (latest)

fleet quarry
#

HI. i have a python api that is sending data to a reactjs frontend. the issue that I am facing is that after I generate content with gpt, I am unable send the full data. the only parts that are sending are the first lines.

ie;
generated data:

Hey bro,

Just wanted to let you know that Mom is planning a family gathering next month. It's going to be at her place on the 15th at 5pm. Make sure to mark your calendar!

Looking forward to catching up with everyone.

Best,
[Your Name]```

sent data:
```Subject: Update on upcoming family gathering ```

the code that sends the data is as follows:

```py

@app.route('/api/stream-results/<model>', methods=['GET'])
def stream_results(model):
    def generate():
        while not streaming_results[model]:
            time.sleep(1)
        s = f"data: {streaming_results[model]}\n\n"
        print(s)
        return s
    
    return Response(generate(), mimetype='text/event-stream')```
when i print s, it shows the propper and expected result, whereas when the frontend receives data, only the first line is received.

if you need frontend code please lmk im happy to provide.
bronze hingeBOT
#

@fleet quarry

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

fleet quarry
#

important part of the receiving function:

                        const eventSourceGpt = new EventSource('http://127.0.0.1:5000/api/stream-results/gpt');
                        eventSourceGpt.onmessage = (e) => {
                            console.log(e.data)
                            setAiResult(prevState => ({ ...prevState, gptResponse: e.data }));
                            eventSourceGpt.close();
                        };```
#

e.data prints the first line

#

my guess of accusing Response() was that right before the sending it was okay, but right after receiving it was broken (the string)

empty gyro
#

Is the large blob of generated data the output of print(s) in your flask route function?

fleet quarry
#

can u explain what flask route function means

empty gyro
#
@app.route('/api/stream-results/<model>', methods=['GET'])
def stream_results(model):
    def generate():
        while not streaming_results[model]:
            time.sleep(1)
        s = f"data: {streaming_results[model]}\n\n"
        print(s)
        return s

This is your flask route. Is the output you put in your original post the output of this print(s) here?

fleet quarry
#

oh yeah, thats the result of s

empty gyro
#

Not the single line, but the whole blob?

fleet quarry
#

i just removed the data: from the original text

#
Hey bro,

Just wanted to let you know that Mom is planning a family gathering next month. It's going to be at her place on the 15th at 5pm. Make sure to mark your calendar!

Looking forward to catching up with everyone.

Best,
[Your Name]```
#

to be clear ^^ that block was the result of print(s)

fleet quarry
empty gyro
#

Hmm, I'm not familiar with EventSource is javascript but from the docs it should be pulling the entire response.

#

For giggles, what happens if you return s.replace("\n", "") in the route? Remove all the newlines just to see if we can narrow down what might be causing the issue.

fleet quarry
#

good idea

#

some odd stuff is going on

#

its running into an infinite loop now

#

even when i yield instead of return

#

im making this very complex for no reason. trying to thread 3 api calls instead of one after another. saving prolly less than a second πŸ’€

empty gyro
#

Make it work first, then make it work complexly.

fleet quarry
#

okay yeah

#

this works

#

now i can just replace with some obscure character

#

then replace back in javascript

#

good thinking preoats

#

preocts

bronze hingeBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.