Skip to content

OpenAI Azure chat.completions.create with stream=True stalling without throwing exception #2319

@begilbert238

Description

@begilbert238

Confirm this is an issue with the Python library and not an underlying OpenAI API

  • This is an issue with the Python library

Describe the bug

I have a fastapi endpoint which streams a response from Azure OpenAI:

@router.post("/message")
def message(sessionID: str = Form(...), messages: str = Form(...), selectedModel: str = Form(...), file: UploadFile = Form(None)):
    message_list = json.loads(messages)
    return StreamingResponse(stream_chat(message_list, sessionID, selectedModel))

def stream_chat(messages: list[ChatCompletionMessageParam], session_id: str, selected_model: str) -> Generator[StreamingResponseChunk, None, None]:
    print('entered stream_chat')
    completion_stream = azure_openai_client.chat.completions.create(
        model=selected_model,
        messages=messages,
        stream=True,
        timeout=45,
        **DEFAULT_MODEL_CONFIG
    )
    print('completion_stream created!')
    #. . . 

On occasion, azure_openai_client.chat.completions.create will infinitely stall, and cause every subsequent request to infinitely stall as well. Once the error occurs, every request reaches 'entered stream_chat' but not 'completion_stream created!', until I reset the FastAPI instance. No exceptions are raised, even after hitting the 45 second timeout. I haven't been able to recreate the bug consistently either. What on earth is going on?
For reference, here are the debug logs when the error starts:

DEBUG:openai._base_client:Sending HTTP Request: POST https://[my-deployment]/openai/deployments/gpt-4o/chat/completions?api-version=2024-08-01-preview
DEBUG:httpcore.http11:receive_response_body.failed exception=GeneratorExit()
DEBUG:httpcore.http11:response_closed.started
DEBUG:httpcore.http11:response_closed.complete

To Reproduce

I am unsure how to reproduce consistently, it seems to happen at random, fairly rarely.

Code snippets

OS

macOS

Python version

Python v3.11.4

Library version

openai v1.75.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Azurefor issues relating to the Azure OpenAI servicebugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions