OpenAI Python Library

repository·main·Indexed 12 days ago

https://github.com/openai/openai-python

The official Python library for the OpenAI API, providing a type-safe interface for synchronous and asynchronous workflows. Version 3.0.0 supports the Responses API, Chat Completions API, and Realtime API for low-latency conversations. Features include workload identity authentication for Kubernetes, Azure, and GCP, SSE streaming, automatic pagination, and webhook signature verification.

Tokens
33.9K
Snippets
108
Records
146
Agent score
98%

What's inside OpenAI

  1. Differences between `.parse()` and `.create()`

    main

    The chat.completions.parse() method imposes stricter constraints than the standard chat.completions.create() method:

    1. Error Handling: If the completion finishes due to length or content_filter, the SDK will raise LengthFinishReasonError or ContentFilterFinishReasonError instead of returning a standard completion object.
    2. Tool Strictness: Only strict function tools are supported (e.g., tools where "strict": True is explicitly set).
  2. Differentiate between `None` as `null` and `None` as a missing field

    main

    In API responses, a field might be explicitly null in JSON or missing entirely. Both result in None in Python. To distinguish them, check the .model_fields_set attribute on the response object.

    if response.my_field is None:
      if 'my_field' not in response.model_fields_set:
        print('Got json like {}, without a "my_field" key present at all.')
      else:
        print('Got json like {"my_field": null}.')
  3. Work with TypedDicts and Pydantic models

    main

    The library uses TypedDict for nested request parameters to provide autocomplete and type safety. Responses are returned as Pydantic models, which include helper methods:

    • model.to_json(): Serializes the model to a JSON string.
    • model.to_dict(): Converts the model to a dictionary.
  4. Handle paginated list responses

    main

    List methods in the OpenAI API return paginated results. The library provides two ways to handle this:

    1. Auto-pagination: Use the returned iterator to automatically fetch successive pages as you iterate. This works for both synchronous (OpenAI) and asynchronous (AsyncOpenAI) clients.
    2. Manual pagination: For granular control, use the following methods on the page object:
      • .has_next_page(): Returns a boolean indicating if more data exists.
      • .next_page_info(): Returns the cursor/details needed for the next page.
      • .get_next_page(): Fetches the next page of results.

    You can also access the cursor directly via the .after property on the page object.

    from openai import OpenAI
    
    client = OpenAI()
    
    # Auto-pagination: fetches more pages as needed
    for job in client.fine_tuning.jobs.list(limit=20):
        print(job)
  5. Access Request IDs for debugging

    main

    Every object response provides a public _request_id property, which corresponds to the x-request-id header. This is useful for logging and reporting issues to OpenAI.

    Important: For failed requests, you must catch the openai.APIStatusError exception to access the request_id property on the exception object itself.

    # From a successful response
    response = await client.responses.create(model="gpt-5.5", input="...")
    print(response._request_id)
    
    # From a failed request
    import openai
    try:
        await client.chat.completions.create(model="gpt-5.5", messages=[...])
    except openai.APIStatusError as exc:
        print(exc.request_id)  # Access ID from the exception
        raise exc
  6. Use the Responses API to generate text

    main

    The Responses API is the primary API for interacting with OpenAI models. You can instantiate the OpenAI client and use client.responses.create() to generate text. By default, the client looks for an OPENAI_API_KEY environment variable.

    import os
    from openai import OpenAI
    
    client = OpenAI(
        # This is the default and can be omitted
        api_key=os.environ.get("OPENAI_API_KEY"),
    )
    
    response = client.responses.create(
        model="gpt-5.5",
        instructions="You are a coding assistant that talks like a pirate.",
        input="How do I check if a Python object is an instance of a class?",
    )
    
    print(response.output_text)
  7. Configure Mutual TLS (mTLS)

    main

    For API-key authenticated requests requiring mTLS, configure a native ssl.SSLContext and pass it to a DefaultHttpx2Client.

    Important requirements:

    • Select the mTLS endpoint explicitly via base_url (e.g., https://mtls.api.openai.com/v1).
    • Disable redirects (follow_redirects=False) in the HTTP client to prevent leaking the certificate to other origins.
    • Provide a complete, leaf-first client-chain PEM in the certfile.
    • The certificate-bearing client is transport-wide; do not reuse it for other services.
    import os
    import ssl
    from openai import OpenAI, DefaultHttpx2Client
    
    ssl_context = ssl.create_default_context(
        cafile=os.environ.get("OPENAI_MTLS_CA_BUNDLE"),
    )
    ssl_context.load_cert_chain(
        certfile=os.environ["OPENAI_MTLS_CERTIFICATE_CHAIN"],
        keyfile=os.environ["OPENAI_MTLS_PRIVATE_KEY"],
        password=os.environ.get("OPENAI_MTLS_PRIVATE_KEY_PASSWORD"),
    )
    
    client = OpenAI(
        api_key=os.environ["OPENAI_API_KEY"],
        base_url=os.environ.get(
            "OPENAI_BASE_URL",
            "https://mtls.api.openai.com/v1",
        ),
        http_client=DefaultHttpx2Client(
            verify=ssl_context,
            follow_redirects=False,
        ),
    )
  8. Use Vision capabilities with the Responses API

    main

    You can pass images to the responses.create method by including them in the input list. Supported formats include image URLs or base64 encoded strings. Use the type input_image for images and input_text for text prompts.

    # Using an image URL
    response = client.responses.create(
        model="gpt-5.5",
        input=[
            {
                "role": "user",
                "content": [
                    {"type": "input_text", "text": "What is in this image?"},
                    {"type": "input_image", "image_url": "https://example.com/image.jpg"},
                ],
            }
        ],
    )
    
    # Using a base64 encoded string
    import base64
    with open("path/to/image.png", "rb") as image_file:
        b64_image = base64.b64encode(image_file.read()).decode("utf-8")
    
    response = client.responses.create(
        model="gpt-5.5",
        input=[
            {
                "role": "user",
                "content": [
                    {"type": "input_text", "text": "What is in this image?"},
                    {"type": "input_image", "image_url": f"data:image/png;base64,{b64_image}"},
                ],
            }
        ],
    )
  9. Stream responses using Server Side Events (SSE)

    main

    You can stream responses by setting stream=True in the create method. This returns an iterator that yields events. This works for both synchronous and asynchronous clients.

    # Synchronous streaming
    from openai import OpenAI
    client = OpenAI()
    stream = client.responses.create(
        model="gpt-5.5",
        input="Write a story.",
        stream=True,
    )
    for event in stream:
        print(event)
    
    # Asynchronous streaming
    import asyncio
    from openai import AsyncOpenAI
    async def main():
        client = AsyncOpenAI()
        stream = await client.responses.create(
            model="gpt-5.5",
            input="Write a story.",
            stream=True,
        )
        async for event in stream:
            print(event)
    
    asyncio.run(main())
  10. Manage HTTP resources and client lifecycle

    main

    The library closes underlying HTTP connections when the client is garbage collected. To manage resources explicitly, use a context manager or call the .close() method.

    from openai import OpenAI
    
    with OpenAI() as client:
      # make requests here
      ...
    
    # HTTP client is now closed