|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +"""0.21 -> 0.22 LangChain config migration helper. |
| 17 | +
|
| 18 | +Detects LangChain Python-side flags in ``model.parameters`` when the |
| 19 | +default framework is active and raises a clear error at LLMRails |
| 20 | +construction, so a stale 0.21 LangChain config surfaces during init |
| 21 | +rather than as an opaque HTTP 400 deep in a guardrail call. |
| 22 | +
|
| 23 | +Remove in 0.23.0. After 0.23 any unrecognized parameter is forwarded |
| 24 | +verbatim to the OpenAI-compatible HTTP client; the wire's HTTP 400 is |
| 25 | +the user's signal to clean up. |
| 26 | +""" |
| 27 | + |
| 28 | +# TODO(0.23): delete this module along with its call site in |
| 29 | +# nemoguardrails.rails.llm.llmrails.LLMRails._init_llms. |
| 30 | + |
| 31 | +import re |
| 32 | +from typing import TYPE_CHECKING, Iterable, List, Optional, Tuple |
| 33 | + |
| 34 | +if TYPE_CHECKING: |
| 35 | + from nemoguardrails.rails.llm.config import Model |
| 36 | + |
| 37 | +_LANGCHAIN_BASE_FLAGS = frozenset( |
| 38 | + { |
| 39 | + "streaming", |
| 40 | + "disable_streaming", |
| 41 | + "verbose", |
| 42 | + "cache", |
| 43 | + "callbacks", |
| 44 | + "tags", |
| 45 | + "metadata", |
| 46 | + "name", |
| 47 | + "model_kwargs", |
| 48 | + } |
| 49 | +) |
| 50 | + |
| 51 | +_PROVIDER_PREFIXED_ALIAS = re.compile(r"^(?P<prefix>[a-zA-Z]\w*?)_(?P<canonical>api_key|base_url|api_base|endpoint)$") |
| 52 | + |
| 53 | + |
| 54 | +def _canonical_name_for(matched_canonical: str) -> str: |
| 55 | + if matched_canonical == "api_key": |
| 56 | + return "api_key" |
| 57 | + return "base_url" |
| 58 | + |
| 59 | + |
| 60 | +def _detect_provider_alias(name: str) -> Optional[str]: |
| 61 | + match = _PROVIDER_PREFIXED_ALIAS.fullmatch(name) |
| 62 | + if match is None: |
| 63 | + return None |
| 64 | + return _canonical_name_for(match.group("canonical")) |
| 65 | + |
| 66 | + |
| 67 | +def _violations_for(model_type: str, parameters: dict) -> List[Tuple[str, str]]: |
| 68 | + """Return a list of (model_type, action) tuples for one model.""" |
| 69 | + out: List[Tuple[str, str]] = [] |
| 70 | + for flag in sorted(_LANGCHAIN_BASE_FLAGS & set(parameters)): |
| 71 | + if flag == "model_kwargs": |
| 72 | + out.append((model_type, "unpack `model_kwargs` contents directly into `parameters`")) |
| 73 | + else: |
| 74 | + out.append((model_type, f"remove `{flag}`")) |
| 75 | + for name in sorted(parameters): |
| 76 | + if name in _LANGCHAIN_BASE_FLAGS: |
| 77 | + continue |
| 78 | + canonical = _detect_provider_alias(name) |
| 79 | + if canonical is None: |
| 80 | + continue |
| 81 | + out.append((model_type, f"rename `{name}` to `{canonical}`")) |
| 82 | + return out |
| 83 | + |
| 84 | + |
| 85 | +def check_langchain_kwargs(models: "Iterable[Model]", active_framework: str) -> None: |
| 86 | + """Raise ValueError if any model carries LangChain Python-side flags. |
| 87 | +
|
| 88 | + No-op when the active framework is anything other than ``default``; |
| 89 | + LangChain-flavored kwargs are valid on the LangChain framework. |
| 90 | + """ |
| 91 | + if active_framework != "default": |
| 92 | + return |
| 93 | + violations: List[Tuple[str, str]] = [] |
| 94 | + for model in models: |
| 95 | + if not model.parameters: |
| 96 | + continue |
| 97 | + violations.extend(_violations_for(model.type, model.parameters)) |
| 98 | + if not violations: |
| 99 | + return |
| 100 | + body = "\n".join(f" models[{model_type}]: {action}" for model_type, action in violations) |
| 101 | + raise ValueError( |
| 102 | + "Your config uses 0.21-style LangChain conventions that the default framework\n" |
| 103 | + "doesn't forward:\n\n" |
| 104 | + f"{body}\n\n" |
| 105 | + "Two paths:\n" |
| 106 | + " - Adapt to the default framework: apply the renames/removals above.\n" |
| 107 | + " Only do this if your endpoint is OpenAI-compatible.\n" |
| 108 | + " - Keep 0.21 LangChain behavior: set NEMOGUARDRAILS_LLM_FRAMEWORK=langchain.\n\n" |
| 109 | + "(Migration check; removed in 0.23.0.)" |
| 110 | + ) |
0 commit comments