# Streaming method - returns SSE event iterator
stream = s.chat.create(prompt="Hello world", stream=True)
for event in stream:
    print(event.data)  # Fully typed ChatEvent
```

This eliminates the need for runtime type checking and provides better IDE support with accurate type hints for both streaming and non-streaming use cases.

## Async vs Sync Methods

Speakeasy-generated Python SDKs provide both synchronous and asynchronous methods for all endpoints. The SDK uses the `httpx` library for making HTTP requests, which supports both synchronous and asynchronous requests.

Synchronous:

```python
res = s.orders.create_order(drinks=[
    Drink(
        type=bar.OrderType.INGREDIENT,
        product_code="AC-A2DF3",
        quantity=138554,
    ),
])
```

Asynchronous:

```python
res = await s.orders.create_order_async(drinks=[
    Drink(
        type=bar.OrderType.INGREDIENT,
        product_code="AC-A2DF3",
        quantity=138554,
    ),
])
```

<Callout type="info" title="Constructor-based async">
Python SDKs can also be configured to use a constructor-based async pattern with separate `AsyncMyAPI` classes. See the [asyncMode configuration option](/docs/speakeasy-reference/generation/python-config#async-method-configuration) for details.
</Callout>


## HTTP Client

To make API calls, the Python SDK instantiates its own HTTP client using the `Client` class from the `httpx` library. This allows authentication settings to persist across requests and reduce overhead.

## Parameters

If configured, Speakeasy will generate methods with parameters for each parameter defined in the OpenAPI document, as long as the number of parameters is less than or equal to the configured `maxMethodParams` value in the `gen.yaml` file.

If the number of parameters exceeds the configured `maxMethodParams` value or is set to `0`, a request object will be generated for the method to pass in all parameters as a single object.

## Errors

The Python SDK will raise exceptions for any network or invalid request errors.

For unsuccessful responses, if a custom error response is specified in the spec file, the SDK will unmarshal the HTTP response details into the custom error response to be thrown as an exception. When no custom response is specified in the spec, the SDK will throw an `SDKException` with details of the failed response.

```python
import sdk
from sdk.models import errors

s = sdk.SDK()
res = None
try:
    res = s.errors.status_get_x_speakeasy_errors(status_code=385913)
except errors.StatusGetXSpeakeasyErrorsResponseBody as e:
    # handle exception
except errors.SDKError as e:
    # handle exception

if res is not None:
    # handle response
    pass
```

## User Agent Strings

The Python SDK includes a [user agent](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent) string in all requests. This can be leveraged to track SDK usage amongst broader API usage. The format is as follows:

```stmpl
speakeasy-sdk/python {{SDKVersion}} {{GenVersion}} {{DocVersion}} {{PackageName}}
```

Where

- `SDKVersion` is the version of the SDK, defined in `gen.yaml` and released
- `GenVersion` is the version of the Speakeasy generator
- `DocVersion` is the version of the OpenAPI document
- `PackageName` is the name of the package defined in `gen.yaml`

## SDK Memory footprint and Load time

Speakeasy-generated Python SDKs employ a lazy loading mechanism for their functional components (e.g., groups of operations like `orders`, `users`, etc.) and their associated models. This means these components are not loaded into memory when you initially import the SDK or create an SDK instance. Instead, a specific component is loaded only the first time you access it.

**Example:**

```python
import sdk