Repository: microsoft/autogen
Pull request: Add default_header support
<!-- Thank you for your contribution! Please review https://microsoft.github.io/autogen/docs/Contribute before opening a pull request. -->

<!-- Please add a reviewer to the assignee section when you create a PR. If you don't have the access to it, we will shortly find a reviewer and assign them to your PR. -->

## Why are these changes needed?

<!-- Please give a short summary of the change and the problem this solves. -->

## Related issue number

Closes #5163

## Checks

- [ ] I've included any doc changes needed for https://microsoft.github.io/autogen/. See https://microsoft.github.io/autogen/docs/Contribute#documentation to build and test documentation locally.
- [ ] I've added tests (if relevant) corresponding to the changes introduced in this PR.
- [x] I've made sure all auto checks have passed.


Problem statement:
ChatCompletionClient.load_component support default-headers
### What feature would you like to be added?

ChatCompletionClient.load_component support default-headers

### Why is this needed?

When I use AzureOpenAIChatCompletionClient and pass the below parameters, the code is working fine,
    model_client = AzureOpenAIChatCompletionClient(
         model = "gpt-4o-2024-05-13",
         azure_deployment= 'xxxxxx',
         azure_endpoint = 'xxxxxx',
         api_version = 'xxxx',
         api_key = os.getenv("AZURE_API_KEY"),
         default_headers = {'X-User-Id': os.getlogin()}
     )
but when I use ChatCompletionClient.load_component, and pass the config:
{'provider': 'AzureOpenAIChatCompletionClient', 'config': {'model': 'xxxxx', 'azure_endpoint': 'xxxx', 'azure_deployment': 'xxxxx', 'api_version': 'xxxx', 'api_key': 'xxxxx', 'default_headers': {'X-User-Id': 'xxxxxx'}}}
it seems there is no way can pass the default_headers property, so meet the below error:
openai.PermissionDeniedError: Error code: 403 - {'error': {'code': '403', 'message': 'The X-User-Id request header is now mandatory for cost allocation. Please set the parameter with "xxxxx" for allocating to the end user\'s department or "srv_xxxx" for background jobs.'}}

Hints from issue discussion:
When I add **default_headers** property in _AzureOpenAIClientConfigurationConfigModel_, it can work fine.
class AzureOpenAIClientConfigurationConfigModel(BaseOpenAIClientConfigurationConfigModel):
    # Azure specific
    azure_endpoint: str
    azure_deployment: str | None = None
    api_version: str
    azure_ad_token: str | None = None
    azure_ad_token_provider: ComponentModel | None = None
    **default_headers: dict | None = None**
Would you like to submit a PR to fix this? The default header property is currently not part of the serialized config. 

We need to modify this: https://github.com/microsoft/autogen/blob/1982f1b0ec83cdb1ac4f3c4b35a143d9a215828c/python/packages/autogen-ext/src/autogen_ext/models/openai/config/__init__.py#L70-L71

to include `default_headers`
Can I take this one?

Commit message: Merge branch 'main' into feature/support_default_headers

Patch to review:
diff --git a/python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.py b/python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.py
index ca652d6580c5..e973862dfe91 100644
--- a/python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.py
+++ b/python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.py
@@ -982,6 +982,7 @@ class OpenAIChatCompletionClient(BaseOpenAIChatCompletionClient, Component[OpenA
         temperature (optional, float):
         top_p (optional, float):
         user (optional, str):
+        default_headers (optional, dict[str, str]):  Custom headers; useful for authentication or other custom requirements.
 
 
     To use this client, you must install the `openai` extension:
@@ -1116,6 +1117,8 @@ class AzureOpenAIChatCompletionClient(
         temperature (optional, float):
         top_p (optional, float):
         user (optional, str):
+        default_headers (optional, dict[str, str]):  Custom headers; useful for authentication or other custom requirements.
+
 
 
     To use this client, you must install the `azure` and `openai` extensions:
diff --git a/python/packages/autogen-ext/src/autogen_ext/models/openai/config/__init__.py b/python/packages/autogen-ext/src/autogen_ext/models/openai/config/__init__.py
index a6b484780212..1fe0e57eb8cc 100644
--- a/python/packages/autogen-ext/src/autogen_ext/models/openai/config/__init__.py
+++ b/python/packages/autogen-ext/src/autogen_ext/models/openai/config/__init__.py
@@ -74,6 +74,7 @@ class BaseOpenAIClientConfigurationConfigModel(CreateArgumentsConfigModel):
     max_retries: int | None = None
     model_capabilities: ModelCapabilities | None = None  # type: ignore
     model_info: ModelInfo | None = None
+    default_header: Dict[str, str] | None = None
 
 
 # See OpenAI docs for explanation of these parameters


Code review comments:
diff_hunk: @@ -74,6 +74,7 @@ class BaseOpenAIClientConfigurationConfigModel(CreateArgumentsConfigModel):
     max_retries: int | None = None
     model_capabilities: ModelCapabilities | None = None  # type: ignore
     model_info: ModelInfo | None = None
+    default_header: Dict[str, str] | None = None
path: python/packages/autogen-ext/src/autogen_ext/models/openai/config/__init__.py
text: @user1:
```suggestion
    default_headers: Dict[str, str] | None = None
```