langchain - ✅(Solved) Fix `AzureChatOpenAI` fails to infer profile data from `model`, breaking profile-dependent features [2 pull requests, 2 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
langchain-ai/langchain#36761Fetched 2026-04-17 08:22:58
View on GitHub
Comments
2
Participants
2
Timeline
6
Reactions
0
Timeline (top)
labeled ×3commented ×2issue_type_added ×1
  • I'm using AzureChatOpenAI, either directly or via init_chat_model("azure_openai:...").
  • I expect model profile data like max_input_tokens and structured_output to be inferred from model / model_name, just like ChatOpenAI.
  • Instead, Azure profile inference depends on azure_deployment. If azure_deployment is unset, profile stays None. If azure_deployment is set to a custom alias, profile becomes {} instead of loading the known profile for model="gpt-4o".

This matters because profile is used by profile-dependent features. For example, missing max_input_tokens can prevent middleware that uses fractional token limits from working, and missing structured_output metadata can affect logic that checks model capabilities.

The current Azure profile-loading logic keys off deployment_name instead of the canonical model name. That causes profile lookup to fail in at least two valid Azure usage patterns:

  1. init_chat_model("azure_openai:gpt-4o") init_chat_model passes the parsed model string as model=..., not azure_deployment=..., so the resulting instance has model_name="gpt-4o" and deployment_name=None.
  2. Direct AzureChatOpenAI(...) usage with a custom deployment alias A common Azure setup is azure_deployment="my-deployment", model="gpt-4o". In that case the canonical model name is available, but profile loading still does not use it, so the profile ends up empty.

By contrast, ChatOpenAI resolves profiles from model_name, so equivalent OpenAI usage gets a populated profile.

Potential fix:

  • When Azure loads default profile data, prefer model_name when it is set, and fall back to deployment_name otherwise.
  • That would fix init_chat_model("azure_openai:..."), direct AzureChatOpenAI(model="..."), and Azure deployments whose deployment name differs from the canonical model name.
  • It would preserve the current behavior when azure_deployment already matches a known model identifier, while also fixing cases where the canonical model name is only available via model / model_name.

Error Message

Error Message and Stack Trace (if applicable)

Root Cause

This matters because profile is used by profile-dependent features. For example, missing max_input_tokens can prevent middleware that uses fractional token limits from working, and missing structured_output metadata can affect logic that checks model capabilities.

Fix Action

Fix / Workaround

  • This is a bug, not a usage question.
  • I added a clear and descriptive title that summarizes this issue.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
  • This is not related to the langchain-community package.
  • I posted a self-contained, minimal, reproducible example. A maintainer can copy it and run it AS IS.

Other Dependencies

httpx: 0.28.1 jsonpatch: 1.33 langgraph: 1.1.6 openai: 2.31.0 orjson: 3.11.8 packaging: 26.1 pydantic: 2.13.0 pyyaml: 6.0.3 requests: 2.33.1 requests-toolbelt: 1.0.0 tenacity: 9.1.4 tiktoken: 0.12.0 typing-extensions: 4.15.0 uuid-utils: 0.14.1 xxhash: 3.6.0 zstandard: 0.25.0

PR fix notes

PR #36855: Fix #36761: AzureChatOpenAI fails to infer profile data from `model...

Description (problem / solution / changelog)

Summary

AzureChatOpenAI._resolve_model_profile only looked up the default profile by deployment_name. That breaks profile inference in two common setups:

  • init_chat_model("azure_openai:gpt-4o", ...)init_chat_model forwards the parsed name as model=..., leaving deployment_name unset, so profile stays None.
  • AzureChatOpenAI(azure_deployment="my-alias", model="gpt-4o", ...) — the custom alias has no entry in _MODEL_PROFILES, so profile resolves to {} even though the canonical model name is known.

Without a populated profile, profile-dependent features (fractional token limits in middleware, structured_output capability checks, etc.) cannot reason about the underlying model. ChatOpenAI already resolves profiles from model_name, so OpenAI usage works — only Azure was miskeyed.

Fix

Try model_name first and fall back to deployment_name. Existing behaviour is preserved when deployment_name already matches a known model identifier (e.g. azure_deployment="gpt-4o"), while also covering the two cases above.

def _resolve_model_profile(self) -> ModelProfile | None:
    if self.model_name is not None:
        profile = _get_default_model_profile(self.model_name)
        if profile:
            return profile
    if self.deployment_name is not None:
        return _get_default_model_profile(self.deployment_name) or None
    return None

Tests

Added test_profile_inference in libs/partners/openai/tests/unit_tests/chat_models/test_azure.py covering:

  1. model="gpt-4o" with no azure_deployment — profile populated.
  2. model="gpt-4o" with custom azure_deployment="my-custom-deployment" — profile populated from model_name.
  3. azure_deployment="gpt-4o" with no model — existing fallback behaviour preserved.

Fixes #36761

Disclaimer

This PR was drafted with the help of an AI coding agent. Human review confirmed the scope and verified the fix against the issue's repro before submission.

Changed files

  • libs/partners/openai/langchain_openai/chat_models/azure.py (modified, +12/-0)
  • libs/partners/openai/tests/unit_tests/chat_models/test_azure.py (modified, +30/-0)

PR #36858: fix(openai): infer azure chat profiles from model name

Description (problem / solution / changelog)

Summary

  • Resolve AzureChatOpenAI default profiles from the canonical model name when provided.
  • Fall back to azure_deployment only when no model name is set.
  • Add unit coverage for model-only, deployment alias, deployment fallback, explicit profile, and unknown-profile cases.

Fixes #36761.

Review focus

  • Confirm the intended precedence when model and azure_deployment disagree.

Testing

  • uv run --frozen --group test pytest tests/unit_tests/chat_models/test_azure.py
  • uv run --frozen --group lint ruff check langchain_openai/chat_models/azure.py tests/unit_tests/chat_models/test_azure.py
  • git diff --check

AI assistance was used while preparing this change; the final diff and tests were reviewed before submission.

Changed files

  • libs/partners/openai/langchain_openai/chat_models/azure.py (modified, +2/-0)
  • libs/partners/openai/tests/unit_tests/chat_models/test_azure.py (modified, +91/-0)

Code Example

from langchain.chat_models import init_chat_model
from langchain_openai import AzureChatOpenAI, ChatOpenAI

azure_kwargs = {
    "azure_endpoint": "https://example-resource.openai.azure.com/",
    "api_key": "test",
    "api_version": "2024-02-01",
}

model = init_chat_model("azure_openai:gpt-4o", **azure_kwargs)
print(model.model_name)       # "gpt-4o"
print(model.deployment_name)  # None
print(model.profile)          # None

model = AzureChatOpenAI(model="gpt-4o", **azure_kwargs)
print(model.profile)          # None

model = AzureChatOpenAI(
    azure_deployment="my-deployment",
    model="gpt-4o",
    **azure_kwargs,
)
print(model.profile)          # {}

model = ChatOpenAI(model="gpt-4o", api_key="test")
print(model.profile)          # {"max_input_tokens": 128000, ...}

---

N/A
RAW_BUFFERClick to expand / collapse

Checked other resources

  • This is a bug, not a usage question.
  • I added a clear and descriptive title that summarizes this issue.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
  • This is not related to the langchain-community package.
  • I posted a self-contained, minimal, reproducible example. A maintainer can copy it and run it AS IS.

Package (Required)

  • langchain
  • langchain-openai
  • langchain-anthropic
  • langchain-classic
  • langchain-core
  • langchain-model-profiles
  • langchain-tests
  • langchain-text-splitters
  • langchain-chroma
  • langchain-deepseek
  • langchain-exa
  • langchain-fireworks
  • langchain-groq
  • langchain-huggingface
  • langchain-mistralai
  • langchain-nomic
  • langchain-ollama
  • langchain-openrouter
  • langchain-perplexity
  • langchain-qdrant
  • langchain-xai
  • Other / not sure / general

Related Issues / PRs

No response

Reproduction Steps / Example Code (Python)

from langchain.chat_models import init_chat_model
from langchain_openai import AzureChatOpenAI, ChatOpenAI

azure_kwargs = {
    "azure_endpoint": "https://example-resource.openai.azure.com/",
    "api_key": "test",
    "api_version": "2024-02-01",
}

model = init_chat_model("azure_openai:gpt-4o", **azure_kwargs)
print(model.model_name)       # "gpt-4o"
print(model.deployment_name)  # None
print(model.profile)          # None

model = AzureChatOpenAI(model="gpt-4o", **azure_kwargs)
print(model.profile)          # None

model = AzureChatOpenAI(
    azure_deployment="my-deployment",
    model="gpt-4o",
    **azure_kwargs,
)
print(model.profile)          # {}

model = ChatOpenAI(model="gpt-4o", api_key="test")
print(model.profile)          # {"max_input_tokens": 128000, ...}

Error Message and Stack Trace (if applicable)

N/A

Description

  • I'm using AzureChatOpenAI, either directly or via init_chat_model("azure_openai:...").
  • I expect model profile data like max_input_tokens and structured_output to be inferred from model / model_name, just like ChatOpenAI.
  • Instead, Azure profile inference depends on azure_deployment. If azure_deployment is unset, profile stays None. If azure_deployment is set to a custom alias, profile becomes {} instead of loading the known profile for model="gpt-4o".

This matters because profile is used by profile-dependent features. For example, missing max_input_tokens can prevent middleware that uses fractional token limits from working, and missing structured_output metadata can affect logic that checks model capabilities.

The current Azure profile-loading logic keys off deployment_name instead of the canonical model name. That causes profile lookup to fail in at least two valid Azure usage patterns:

  1. init_chat_model("azure_openai:gpt-4o") init_chat_model passes the parsed model string as model=..., not azure_deployment=..., so the resulting instance has model_name="gpt-4o" and deployment_name=None.
  2. Direct AzureChatOpenAI(...) usage with a custom deployment alias A common Azure setup is azure_deployment="my-deployment", model="gpt-4o". In that case the canonical model name is available, but profile loading still does not use it, so the profile ends up empty.

By contrast, ChatOpenAI resolves profiles from model_name, so equivalent OpenAI usage gets a populated profile.

Potential fix:

  • When Azure loads default profile data, prefer model_name when it is set, and fall back to deployment_name otherwise.
  • That would fix init_chat_model("azure_openai:..."), direct AzureChatOpenAI(model="..."), and Azure deployments whose deployment name differs from the canonical model name.
  • It would preserve the current behavior when azure_deployment already matches a known model identifier, while also fixing cases where the canonical model name is only available via model / model_name.

System Info

System Information

OS: Darwin OS Version: Darwin Kernel Version 25.3.0: Wed Jan 28 20:51:28 PST 2026; root:xnu-12377.91.3~2/RELEASE_ARM64_T6041 Python Version: 3.14.3 (main, Feb 12 2026, 00:29:58) [Clang 21.1.4 ]

Package Information

langchain_core: 1.2.29 langchain: 1.2.15 langsmith: 0.7.31 langchain_openai: 1.1.11 langgraph_sdk: 0.3.13

Optional packages not installed

deepagents deepagents-cli

Other Dependencies

httpx: 0.28.1 jsonpatch: 1.33 langgraph: 1.1.6 openai: 2.31.0 orjson: 3.11.8 packaging: 26.1 pydantic: 2.13.0 pyyaml: 6.0.3 requests: 2.33.1 requests-toolbelt: 1.0.0 tenacity: 9.1.4 tiktoken: 0.12.0 typing-extensions: 4.15.0 uuid-utils: 0.14.1 xxhash: 3.6.0 zstandard: 0.25.0

extent analysis

TL;DR

The issue can be resolved by modifying the Azure profile loading logic to prefer model_name when it is set, and fall back to deployment_name otherwise.

Guidance

  • The current implementation of AzureChatOpenAI relies on azure_deployment to load the model profile, which can lead to incorrect or empty profiles when azure_deployment is not set or does not match the canonical model name.
  • To fix this, the AzureChatOpenAI class should be modified to use model_name as the primary key for loading the model profile, and only fall back to deployment_name if model_name is not set.
  • This change would ensure that the correct model profile is loaded in all valid Azure usage patterns, including init_chat_model("azure_openai:gpt-4o") and direct AzureChatOpenAI usage with a custom deployment alias.
  • The modified logic should preserve the current behavior when azure_deployment already matches a known model identifier.

Example

class AzureChatOpenAI:
    # ...
    def __init__(self, model, azure_deployment=None, **kwargs):
        # ...
        if self.model_name:
            # Load profile using model_name
            self.profile = load_profile(self.model_name)
        elif self.deployment_name:
            # Fall back to deployment_name if model_name is not set
            self.profile = load_profile(self.deployment_name)
        else:
            # Handle the case where neither model_name nor deployment_name is set
            self.profile = {}

Notes

  • This solution assumes that the load_profile function is already implemented and can load the correct model profile given a model name or deployment name.
  • The exact implementation details may vary depending on the specific requirements and constraints of the langchain_openai package.

Recommendation

Apply the workaround by modifying the AzureChatOpenAI class to prefer model_name when loading the model profile, as described in the guidance section. This change should fix the issue and ensure that the correct model profile is loaded in all valid Azure usage patterns.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING