Skip to content

Commit 30e2260

Browse files
dumko2001ioop
andauthored
fix(core): Decouple provider prefix from model name in init_chat_mode… (#34046)
:…l logic Addresses Issue #34007. Fixes a bug where aliases like 'mistral:' were inferred correctly as a provider but the prefix was not stripped from the model name, causing API 400 errors. Added logic to strip prefix when inference succeeds. **Description** This PR resolves a logic error in `init_chat_model` where inferred provider aliases (specifically `mistral:`) were correctly identified but not stripped from the model string. **The Problem** When passing a string like `mistral:ministral-8b-latest`, the factory logic correctly inferred the provider as `mistralai` but failed to enter the string-splitting block because the alias `mistral` was not in the hardcoded `_SUPPORTED_PROVIDERS` list. This caused the raw string `mistral:ministral-8b-latest` to be passed to the `ChatMistralAI` constructor, resulting in a 400 API error. **The Fix** I updated `_parse_model` in `libs/langchain/langchain/chat_models/base.py`. The logic now attempts to infer the provider from the prefix *before* determining whether to split the string. This ensures that valid aliases trigger the stripping logic, passing only the clean `model_name` to the integration class. **Issue** Fixes #34007 **Dependencies** None. **Verification** Validated locally with a reproduction script: - Input: `mistral:ministral-8b-latest` - Result: Successfully instantiates `ChatMistralAI` with `model="ministral-8b-latest"`. - Validated that standard inputs (e.g., `gpt-4o`) remain unaffected. Co-authored-by: ioop <[email protected]>
1 parent cbaea35 commit 30e2260

File tree

1 file changed

+11
-7
lines changed
  • libs/langchain/langchain_classic/chat_models

1 file changed

+11
-7
lines changed

libs/langchain/langchain_classic/chat_models/base.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -551,13 +551,17 @@ def _attempt_infer_model_provider(model_name: str) -> str | None:
551551

552552

553553
def _parse_model(model: str, model_provider: str | None) -> tuple[str, str]:
554-
if (
555-
not model_provider
556-
and ":" in model
557-
and model.split(":")[0] in _SUPPORTED_PROVIDERS
558-
):
559-
model_provider = model.split(":")[0]
560-
model = ":".join(model.split(":")[1:])
554+
if not model_provider and ":" in model:
555+
prefix, suffix = model.split(":", 1)
556+
if prefix in _SUPPORTED_PROVIDERS:
557+
model_provider = prefix
558+
model = suffix
559+
else:
560+
inferred = _attempt_infer_model_provider(prefix)
561+
if inferred:
562+
model_provider = inferred
563+
model = suffix
564+
561565
model_provider = model_provider or _attempt_infer_model_provider(model)
562566
if not model_provider:
563567
msg = (

0 commit comments

Comments
 (0)