langchain - ✅(Solved) Fix Unsupported context management with openai and azure endpoint [1 pull requests, 4 comments, 5 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#35464Fetched 2026-04-08 00:26:02
View on GitHub
Comments
4
Participants
5
Timeline
13
Reactions
0
Author
Timeline (top)
commented ×4labeled ×4subscribed ×2cross-referenced ×1

Trying to use azure endpoint with context_management=[{"type": "compaction", "compact_threshold": 100_000}] returns a compact_threshold is not enabled error

Error Message

from langchain_openai import ChatOpenAI

try: _llm = ChatOpenAI( model=config["model_name"], base_url=f"{config['azure_endpoint']}/openai/v1/", api_key=config["azure_api_key"], reasoning={ "effort": "high", # 'low', 'medium', or 'high' "summary": "auto", # 'detailed', 'auto', or None }, use_previous_response_id=True, use_responses_api=True, context_management=[{"type": "compaction", "compact_threshold": 100_000}], )

response = _llm.invoke([{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "tell me the best way to optimize a function in python?"}])

except Exception as e: print(f"Error: {e}")

Root Cause

Trying to use azure endpoint with context_management=[{"type": "compaction", "compact_threshold": 100_000}] returns a compact_threshold is not enabled error

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.0.9 openai: 2.24.0 orjson: 3.11.7 packaging: 26.0 pydantic: 2.12.5 pytest: 8.4.2 pyyaml: 6.0.3 requests: 2.32.5 requests-toolbelt: 1.0.0 rich: 14.3.3 tenacity: 9.1.4 tiktoken: 0.12.0 typing-extensions: 4.15.0 uuid-utils: 0.14.1 xxhash: 3.6.0

PR fix notes

PR #36022: fix(openai): skip context_management for Azure endpoint (#35464)

Description (problem / solution / changelog)

Summary

Fixes #35464

Azure endpoint does not support context_management (e.g. compact_threshold). When base_url contains "azure", exclude context_management from API params to prevent compact_threshold is not enabled error.

Root cause

ChatOpenAI with base_url pointing to Azure and context_management=[{"type": "compaction", "compact_threshold": 100_000}] sends the param to the API. Azure returns 400: compact_threshold is not enabled.

Changes

  • In _default_params, when openai_api_base contains "azure", set context_management to None so it is excluded from the request.
  • Added unit tests for Azure and non-Azure cases.

Testing

  • test_context_management_excluded_for_azure_base_url: verifies context_management is not in params when base_url contains azure
  • test_context_management_included_for_non_azure_base_url: verifies context_management is passed for non-Azure endpoints

Made with Cursor

Changed files

  • libs/partners/openai/langchain_openai/chat_models/base.py (modified, +10/-1)
  • libs/partners/openai/tests/unit_tests/chat_models/test_base.py (modified, +24/-0)

Code Example

from langchain_openai import ChatOpenAI

try:
    _llm = ChatOpenAI(
        model=config["model_name"],
        base_url=f"{config['azure_endpoint']}/openai/v1/",
        api_key=config["azure_api_key"],
        reasoning={
        "effort": "high",  # 'low', 'medium', or 'high'
        "summary": "auto",  # 'detailed', 'auto', or None
        },
        use_previous_response_id=True,
        use_responses_api=True,
        context_management=[{"type": "compaction", "compact_threshold": 100_000}],
    )

    response = _llm.invoke([{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "tell me the best way to optimize a function in python?"}])

except Exception as e:
    print(f"Error: {e}")

---

Error: Error code: 400 - {'error': {'message': 'compact_threshold is not enabled.', 'type': 'invalid_request_error', 'param': 'compact_threshold', 'code': 'unsupported_parameter'}}
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_openai import ChatOpenAI

try:
    _llm = ChatOpenAI(
        model=config["model_name"],
        base_url=f"{config['azure_endpoint']}/openai/v1/",
        api_key=config["azure_api_key"],
        reasoning={
        "effort": "high",  # 'low', 'medium', or 'high'
        "summary": "auto",  # 'detailed', 'auto', or None
        },
        use_previous_response_id=True,
        use_responses_api=True,
        context_management=[{"type": "compaction", "compact_threshold": 100_000}],
    )

    response = _llm.invoke([{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "tell me the best way to optimize a function in python?"}])

except Exception as e:
    print(f"Error: {e}")

Error Message and Stack Trace (if applicable)

Error: Error code: 400 - {'error': {'message': 'compact_threshold is not enabled.', 'type': 'invalid_request_error', 'param': 'compact_threshold', 'code': 'unsupported_parameter'}}

Description

Trying to use azure endpoint with context_management=[{"type": "compaction", "compact_threshold": 100_000}] returns a compact_threshold is not enabled error

System Info

System Information

OS: Windows OS Version: 10.0.26200 Python Version: 3.14.3 (tags/v3.14.3:323c59a, Feb 3 2026, 16:04:56) [MSC v.1944 64 bit (AMD64)]

Package Information

langchain_core: 1.2.16 langchain: 1.2.10 langsmith: 0.7.7 langchain_openai: 1.1.10 langgraph_sdk: 0.3.9

Optional packages not installed

deepagents deepagents-cli

Other Dependencies

httpx: 0.28.1 jsonpatch: 1.33 langgraph: 1.0.9 openai: 2.24.0 orjson: 3.11.7 packaging: 26.0 pydantic: 2.12.5 pytest: 8.4.2 pyyaml: 6.0.3 requests: 2.32.5 requests-toolbelt: 1.0.0 rich: 14.3.3 tenacity: 9.1.4 tiktoken: 0.12.0 typing-extensions: 4.15.0 uuid-utils: 0.14.1 xxhash: 3.6.0

extent analysis

Fix Plan

1. Disable Compact Threshold

The error message indicates that the compact_threshold parameter is not enabled. To fix this, you can remove the context_management parameter from the ChatOpenAI initialization.

2. Update Code

Replace the following code:

context_management=[{"type": "compaction", "compact_threshold": 100_000}],

with:

# Remove the context_management parameter

3. Verify

Run the code again and verify that the error is resolved.

Example Code

from langchain_openai import ChatOpenAI

try:
    _llm = ChatOpenAI(
        model=config["model_name"],
        base_url=f"{config['azure_endpoint']}/openai/v1/",
        api_key=config["azure_api_key"],
        reasoning={
        "effort": "high",  # 'low', 'medium', or 'high'
        "summary": "auto",  # 'detailed', 'auto', or None
        },
        use_previous_response_id=True,
        use_responses_api=True,
    )

    response = _llm.invoke([{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "tell me the best way to optimize a function in python?"}])

except Exception as e:
    print(f"Error: {e}")

Verification

  1. Run the updated code.
  2. Verify that the error is resolved.
  3. Check the response from the LLM to ensure it is correct.

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

langchain - ✅(Solved) Fix Unsupported context management with openai and azure endpoint [1 pull requests, 4 comments, 5 participants]