Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

fix(llms): let a per-call model= override the LiteLLM constructor model (#2084)

jkbb102dad90f0
diff --git a/semantica/llms/litellm.py b/semantica/llms/litellm.py
index 7e63b56d..100f8113 100644
--- a/semantica/llms/litellm.py
+++ b/semantica/llms/litellm.py
@@ -131,6 +131,21 @@ class LiteLLM:
self.api_key = api_key
self.config = kwargs
+ def _resolve_model(self, options: Dict[str, Any]) -> str:
+ """Pop a per-call ``model`` from ``options`` and return the model to use.
+
+ The per-call value is removed so ``completion()`` does not receive
+ ``model`` twice. It wins only when it is a string that names a model
+ (its ``strip()`` is not empty); anything else - absent, ``None``, empty,
+ whitespace-only or not a string - falls back to ``self.model``. This is
+ the rule ``_track_llm_call`` uses to record the model, so a provenance
+ entry names the model that was actually sent.
+ """
+ model = options.pop("model", None)
+ if isinstance(model, str) and model.strip():
+ return model
+ return self.model
+
def is_available(self) -> bool:
"""Check if LiteLLM provider is available."""
return LITELLM_AVAILABLE
@@ -157,13 +172,14 @@ class LiteLLM:
try:
# Merge config with kwargs
options = {**self.config, **kwargs}
+ model = self._resolve_model(options)
# Prepare messages
messages = [{"role": "user", "content": prompt}]
# Call LiteLLM completion
response = completion(
- model=self.model,
+ model=model,
messages=messages,
api_key=self.api_key,
**options
@@ -229,13 +245,14 @@ class LiteLLM:
# Merge config with kwargs
options = {**self.config, **kwargs}
+ model = self._resolve_model(options)
# Prepare messages
messages = [{"role": "user", "content": json_prompt}]
# Call LiteLLM completion
response = completion(
- model=self.model,
+ model=model,
messages=messages,
api_key=self.api_key,
**options
@@ -301,6 +318,7 @@ class LiteLLM:
)
options = {**self.config, **kwargs}
+ model = self._resolve_model(options)
# Preferred path: instructor drives the schema and retries natively.
# Only an instructor setup failure falls through to the manual loop - a
@@ -319,7 +337,7 @@ class LiteLLM:
if client is not None:
try:
return client.chat.completions.create(
- model=self.model,
+ model=model,
messages=[{"role": "user", "content": prompt}],
api_key=self.api_key,
response_model=schema,
diff --git a/tests/test_llm_litellm.py b/tests/test_llm_litellm.py
index 8ffd5f1d..6d8ee228 100644
--- a/tests/test_llm_litellm.py
+++ b/tests/test_llm_litellm.py
@@ -303,3 +303,73 @@ def test_generate_structured_bracket_in_string_value(litellm_available, monkeypa
)
result = llm.generate_structured("test")
assert result == {"msg": "array: [1,2]", "val": 42}
+
+
+# --- per-call model override (#2056) ----------------------------------------
+
+
[email protected]("method", ["generate", "generate_structured"])
+ "call_kwargs, expected",
+ [({"model": "openai/gpt-4o-mini"}, "openai/gpt-4o-mini"), ({}, "openai/gpt-4o")],
+)
+def test_per_call_model_reaches_completion_once(
+ litellm_available, monkeypatch, method, call_kwargs, expected
+):
+ fake_message = MagicMock()
+ fake_message.content = '{"ok": 1}'
+ fake_response = MagicMock()
+ fake_response.choices = [MagicMock(message=fake_message)]
+ completion = MagicMock(return_value=fake_response)
+ monkeypatch.setattr(litellm_module, "completion", completion)
+
+ llm = LiteLLM(model="openai/gpt-4o", api_key="k")
+ getattr(llm, method)("hello", **call_kwargs)
+
+ assert completion.call_args.kwargs["model"] == expected
+
+
+def test_generate_typed_per_call_model_reaches_instructor(litellm_available, monkeypatch):
+ fake_client = MagicMock()
+ fake_client.chat.completions.create.return_value = _Point(x=1, y=2)
+ _with_instructor(monkeypatch, fake_client)
+
+ llm = LiteLLM(model="openai/gpt-4o", api_key="k")
+ llm.generate_typed("give me a point", _Point, model="openai/gpt-4o-mini")
+
+ _, kwargs = fake_client.chat.completions.create.call_args
+ assert kwargs["model"] == "openai/gpt-4o-mini"
+
+
[email protected]("method", ["generate", "generate_structured"])
[email protected]("blank", ["", " ", "\t\n", None, 123])
+def test_blank_per_call_model_falls_back_to_constructor_model(
+ litellm_available, monkeypatch, method, blank
+):
+ # Same rule as _track_llm_call: a value that names no model is not sent.
+ fake_message = MagicMock()
+ fake_message.content = '{"ok": 1}'
+ fake_response = MagicMock()
+ fake_response.choices = [MagicMock(message=fake_message)]
+ completion = MagicMock(return_value=fake_response)
+ monkeypatch.setattr(litellm_module, "completion", completion)
+
+ llm = LiteLLM(model="openai/gpt-4o", api_key="k")
+ getattr(llm, method)("hello", model=blank)
+
+ assert completion.call_args.kwargs["model"] == "openai/gpt-4o"
+
+
[email protected]("blank", ["", " "])
+def test_generate_typed_blank_per_call_model_falls_back(
+ litellm_available, monkeypatch, blank
+):
+ fake_client = MagicMock()
+ fake_client.chat.completions.create.return_value = _Point(x=1, y=2)
+ _with_instructor(monkeypatch, fake_client)
+
+ llm = LiteLLM(model="openai/gpt-4o", api_key="k")
+ llm.generate_typed("give me a point", _Point, model=blank)
+
+ _, kwargs = fake_client.chat.completions.create.call_args
+ assert kwargs["model"] == "openai/gpt-4o"