refactor: Standardize model name extraction with helper function and tests (#11352)

* add valitador to agent model

* [autofix.ci] apply automated fixes

* [autofix.ci] apply automated fixes (attempt 2/3)

* [autofix.ci] apply automated fixes

* [autofix.ci] apply automated fixes

* [autofix.ci] apply automated fixes (attempt 2/3)

* [autofix.ci] apply automated fixes (attempt 3/3)

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
Cristhian Zanforlin Lousa
2026-01-28 14:52:52 -03:00
committed by GitHub
parent b89bd76e22
commit ed675ea478
21 changed files with 88 additions and 45 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1226,7 +1226,7 @@
},
"Agent": {
"versions": {
"0.3.0": "bdc309bc2d2a"
"0.3.0": "b60d28d2784f"
}
},
"EmbeddingModel": {

View File

@ -1,6 +1,17 @@
from lfx.custom.custom_component.component import Component
def _extract_model_name(value):
"""Extract model name from ModelInput format (list of dicts with 'name' key)."""
if isinstance(value, str):
return value
if isinstance(value, list) and value and isinstance(value[0], dict):
return value[0].get("name")
if isinstance(value, dict):
return value.get("name")
return None
class ChatComponent(Component):
display_name = "Chat Component"
description = "Use as base for chat components."
@ -15,7 +26,11 @@ class ChatComponent(Component):
icon = component.icon
possible_attributes = ["model_name", "model_id", "model"]
for attribute in possible_attributes:
if hasattr(component, attribute) and getattr(component, attribute):
return getattr(component, attribute), icon, source, component.get_id()
if hasattr(component, attribute):
attr_value = getattr(component, attribute)
if attr_value:
model_name = _extract_model_name(attr_value)
if model_name:
return model_name, icon, source, component.get_id()
return source, icon, component.display_name, component.get_id()
return None, None, None, None

View File

@ -451,7 +451,6 @@ class AgentComponent(ToolCallingAgentComponent):
# Iterate over all providers in the MODEL_PROVIDERS_DICT
if field_name == "model":
self.log(str(field_value))
# Update input types for all fields
build_config = self.update_input_types(build_config)

View File

View File

@ -0,0 +1,29 @@
from lfx.base.io.chat import _extract_model_name
class TestExtractModelName:
def test_should_return_string_when_input_is_string(self):
assert _extract_model_name("gpt-4o-mini") == "gpt-4o-mini"
def test_should_return_name_when_input_is_model_input_list(self):
model_input = [{"name": "gpt-4o-mini", "icon": "OpenAI", "provider": "OpenAI"}]
assert _extract_model_name(model_input) == "gpt-4o-mini"
def test_should_return_name_when_input_is_dict(self):
model_dict = {"name": "claude-3", "provider": "Anthropic"}
assert _extract_model_name(model_dict) == "claude-3"
def test_should_return_none_when_input_is_empty_list(self):
assert _extract_model_name([]) is None
def test_should_return_none_when_input_is_none(self):
assert _extract_model_name(None) is None
def test_should_return_none_when_list_has_no_name_key(self):
assert _extract_model_name([{"provider": "OpenAI"}]) is None
def test_should_return_none_when_dict_has_no_name_key(self):
assert _extract_model_name({"provider": "OpenAI"}) is None
def test_should_return_none_when_input_is_integer(self):
assert _extract_model_name(123) is None