KEMBAR78
Add `include_name_in_message` parameter to make `name` field optional in OpenAI messages by Copilot · Pull Request #6845 · microsoft/autogen · GitHub
Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jul 23, 2025

Many OpenAI-compatible model providers (like Groq, Together AI, etc.) don't support the name field in chat completion messages, causing API errors when using AutoGen with these providers.

This PR adds an include_name_in_message parameter to BaseOpenAIChatCompletionClient that controls whether the name field is included in message parameters sent to the API.

Changes

  • Configuration: Added include_name_in_message: bool parameter to BaseOpenAIClientConfiguration and related Pydantic models
  • Client Layer: Updated BaseOpenAIChatCompletionClient, OpenAIChatCompletionClient, and AzureOpenAIChatCompletionClient constructors to handle the new parameter
  • Transformation: Modified _set_name() function to conditionally include the name field based on context
  • Integration: Updated to_oai_type() and count_tokens_openai() functions to pass the parameter through the transformation pipeline
  • Testing: Added comprehensive tests covering various scenarios and model families

Usage

Standard OpenAI (default behavior)

client = OpenAIChatCompletionClient(
    model="gpt-4o",
    api_key="your-openai-key"
    # include_name_in_message defaults to True
)

Groq (disable name field)

client = OpenAIChatCompletionClient(
    model="llama-3.1-70b-versatile",
    base_url="https://api.groq.com/openai/v1",
    api_key="your-groq-key",
    include_name_in_message=False  # Disable name field
)

Alternative: Use name prefixes instead

client = OpenAIChatCompletionClient(
    model="custom-model",
    base_url="https://api.example.com/v1",
    api_key="your-key",
    include_name_in_message=False,  # Don't send name field
    add_name_prefixes=True          # Prepend names to content instead
)

Message Transformation

With name field (include_name_in_message=True):

{
  "role": "user",
  "content": "Hello there!",
  "name": "User123"
}

Without name field (include_name_in_message=False):

{
  "role": "user",
  "content": "Hello there!"
}

Backward Compatibility

This change is fully backward compatible:

  • include_name_in_message defaults to True
  • Existing code continues to work unchanged
  • Only affects behavior when explicitly set to False

The implementation follows existing patterns (similar to how Mistral models already exclude the name field) and provides consistent control across all model types.

Fixes #6034.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 2 commits July 23, 2025 23:02
Co-authored-by: ekzhu <320302+ekzhu@users.noreply.github.com>
… parameter

Co-authored-by: ekzhu <320302+ekzhu@users.noreply.github.com>
Copilot AI changed the title [WIP] Make usage of name field in OpenAI messages optional in OpenAIChatCompletionClient Add include_name_in_message parameter to make name field optional in OpenAI messages Jul 23, 2025
Copilot AI requested a review from ekzhu July 23, 2025 23:07
Copilot finished work on behalf of ekzhu July 23, 2025 23:07
@ekzhu ekzhu marked this pull request as ready for review July 27, 2025 07:04
@codecov
Copy link

codecov bot commented Jul 27, 2025

Codecov Report

❌ Patch coverage is 86.66667% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.92%. Comparing base (ac051ba) to head (456ba42).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...xt/src/autogen_ext/models/openai/_openai_client.py 75.00% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6845   +/-   ##
=======================================
  Coverage   79.91%   79.92%           
=======================================
  Files         235      235           
  Lines       18172    18184   +12     
=======================================
+ Hits        14523    14533   +10     
- Misses       3649     3651    +2     
Flag Coverage Δ
unittests 79.92% <86.66%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ekzhu ekzhu merged commit 5f1c69d into main Jul 27, 2025
72 checks passed
@ekzhu ekzhu deleted the copilot/fix-6034 branch July 27, 2025 07:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make usage of name field in OpenAI messages optional in OpenAIChatCompletionClient

2 participants