# Function calls and streaming

**URL:** <https://community.openai.com/t/function-calls-and-streaming/263393>\
**Category:** API\
**Created:** [June 14, 2023, 2:08am UTC](https://community.openai.com/t/function-calls-and-streaming/263393 "2023-06-14T02:08:57Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![reginald.l](https://avatars.discourse-cdn.com/v4/letter/r/e0b2c6/32.png) [@reginald.l](https://community.openai.com/u/reginald.l)\
**Post date:** [June 14, 2023, 2:08am UTC](https://community.openai.com/t/function-calls-and-streaming/263393/1 "2023-06-14T02:08:57Z")

</div>

Hello,

Is function call incompatible with streaming?  
I tried the example from the [doc](https://platform.openai.com/docs/guides/gpt/function-calling) but as soon as I set `stream=True`, the chat completion does not provide any arguments to a function call.

```auto
{'role': 'assistant', 'content': None, 'function_call': <OpenAIObject at 0x117515770> JSON: {
  "name": "test_function",
  "arguments": ""
}}

```

Am I doing something wrong?

Thanks.

---

<div class="post-metadata">

**Author:** ![msanto](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/msanto/32/430382_2.png) [@msanto](https://community.openai.com/u/msanto)\
**Post date:** [June 14, 2023, 1:51pm UTC](https://community.openai.com/t/function-calls-and-streaming/263393/2 "2023-06-14T13:51:42Z")

</div>

Hi together,

join @reginald.l here.  
I would also like to have function call with streaming.

Kind regards,  
Michele

---

<div class="post-metadata">

**Author:** ![reginald.l](https://avatars.discourse-cdn.com/v4/letter/r/e0b2c6/32.png) [@reginald.l](https://community.openai.com/u/reginald.l)\
**Post date:** [June 14, 2023, 5:53pm UTC](https://community.openai.com/t/function-calls-and-streaming/263393/3 "2023-06-14T17:53:24Z")

</div>

So it turns out that arguments are streamed. In a response there will be multiple `function_call` objects. I ended up doing something like this:

```auto
    func_call = {
        "name": None,
        "arguments": "",
    }
    for res in response:
        delta = res.choices[0].delta
        if "function_call" in delta:
            if "name" in delta.function_call:
                func_call["name"] = delta.function_call["name"]
            if "arguments" in delta.function_call:
                func_call["arguments"] += delta.function_call["arguments"]
        if res.choices[0].finish_reason == "function_call":
            # function call here using func_call
            return
        if not delta.get("content", None):
            continue
        sys.stdout.write(delta.content)
        sys.stdout.flush()

```

---

<div class="post-metadata">

**Author:** ![DarthFader](https://avatars.discourse-cdn.com/v4/letter/d/7ea924/32.png) [@DarthFader](https://community.openai.com/u/DarthFader)\
**Post date:** [June 16, 2023, 4:25pm UTC](https://community.openai.com/t/function-calls-and-streaming/263393/4 "2023-06-16T16:25:50Z")

</div>

Thanks for sharing this. Could we assume the API would never return both non-empty `content` as well as non-empty `function_call` ?

---

<div class="post-metadata">

**Author:** ![DarthFader](https://avatars.discourse-cdn.com/v4/letter/d/7ea924/32.png) [@DarthFader](https://community.openai.com/u/DarthFader)\
**Post date:** [June 16, 2023, 4:40pm UTC](https://community.openai.com/t/function-calls-and-streaming/263393/6 "2023-06-16T16:40:55Z")

</div>

I guess it’s best not to assume this…

ALso in your code I assume you would do `json.loads()` to parse your `arguments`

---

<div class="post-metadata">

**Author:** ![reginald.l](https://avatars.discourse-cdn.com/v4/letter/r/e0b2c6/32.png) [@reginald.l](https://community.openai.com/u/reginald.l)\
**Post date:** [June 16, 2023, 4:41pm UTC](https://community.openai.com/t/function-calls-and-streaming/263393/7 "2023-06-16T16:41:06Z")

</div>

I’m not sure if we can assume that, this is all implicit behavior as far as I can tell.

---

<div class="post-metadata">

**Author:** ![reginald.l](https://avatars.discourse-cdn.com/v4/letter/r/e0b2c6/32.png) [@reginald.l](https://community.openai.com/u/reginald.l)\
**Post date:** [June 16, 2023, 4:43pm UTC](https://community.openai.com/t/function-calls-and-streaming/263393/8 "2023-06-16T16:43:48Z")

</div>

I had another post about `json.loads`, the API doesn’t seem to always return a valid json object but rather a python object (with triple quotes for instance).  
In their examples they use `eval` to parse arguments which, to me, seems incredibly dangerous.  
I would suggest to use `ast.literal_eval` for now, as it is much safer.

---

<div class="post-metadata">

**Author:** ![sam.saffron](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/sam.saffron/32/31387_2.png) [@sam.saffron](https://community.openai.com/u/sam.saffron)\
**Post date:** [June 17, 2023, 5:29am UTC](https://community.openai.com/t/function-calls-and-streaming/263393/9 "2023-06-17T05:29:59Z")

</div>

Agree, in fact when I coded this I assumed we could get both and also assumed that future iterations may return multiple functions (which I have not seen yet)

---

<div class="post-metadata">

**Author:** ![saglanivatsal](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/saglanivatsal/32/141939_2.png) [@saglanivatsal](https://community.openai.com/u/saglanivatsal)\
**Post date:** [November 29, 2023, 5:18pm UTC](https://community.openai.com/t/function-calls-and-streaming/263393/10 "2023-11-29T17:18:31Z")

</div>

One observation that I’ve had is, when streaming function call results the generation is quite slower than normal i.e. without function call. Did anyone observe/face the same or is it just me?

---

<div class="post-metadata">

**Author:** ![anon22939549](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@anon22939549](https://community.openai.com/u/anon22939549)\
**Post date:** [February 6, 2024, 8:02pm UTC](https://community.openai.com/t/function-calls-and-streaming/263393/11 "2024-02-06T20:02:29Z")

</div>

Closed on February 6, 2024, 8:02pm UTC


