Skip to content

Commit aa3d046

Browse files
sjrljulian-risch
authored andcommitted
fix: In State schema validation use != instead of is not for checking the type of messages (#9454)
* Use != instead of is not * Add reno * Use more == instead of is * Fix mypy
1 parent ebeef28 commit aa3d046

4 files changed

Lines changed: 9 additions & 4 deletions

File tree

haystack/components/agents/state/state.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def _validate_schema(schema: Dict[str, Any]) -> None:
6969
raise ValueError(f"StateSchema: 'type' for key '{param}' must be a Python type, got {definition['type']}")
7070
if definition.get("handler") is not None and not callable(definition["handler"]):
7171
raise ValueError(f"StateSchema: 'handler' for key '{param}' must be callable or None")
72-
if param == "messages" and definition["type"] is not List[ChatMessage]:
72+
if param == "messages" and definition["type"] != List[ChatMessage]:
7373
raise ValueError(f"StateSchema: 'messages' must be of type List[ChatMessage], got {definition['type']}")
7474

7575

haystack/components/agents/state/state_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def _is_valid_type(obj: Any) -> bool:
3131
False
3232
"""
3333
# Handle Union types (including Optional)
34-
if hasattr(obj, "__origin__") and obj.__origin__ is Union:
34+
if hasattr(obj, "__origin__") and obj.__origin__ == Union:
3535
return True
3636

3737
# Handle normal classes and generic types
@@ -45,7 +45,7 @@ def _is_list_type(type_hint: Any) -> bool:
4545
:param type_hint: The type hint to check
4646
:return: True if the type hint represents a list, False otherwise
4747
"""
48-
return type_hint is list or (hasattr(type_hint, "__origin__") and get_origin(type_hint) is list)
48+
return type_hint == list or (hasattr(type_hint, "__origin__") and get_origin(type_hint) == list)
4949

5050

5151
def merge_lists(current: Union[List[T], T, None], new: Union[List[T], T]) -> List[T]:

haystack/components/generators/chat/hugging_face_local.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ def run(
408408
replies = [o.get("generated_text", "") for o in output]
409409

410410
# Remove stop words from replies if present
411-
for stop_word in stop_words:
411+
for stop_word in stop_words or []:
412412
replies = [reply.replace(stop_word, "").rstrip() for reply in replies]
413413

414414
chat_messages = [
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
Fix type comparison in schema validation by replacing `is not` with `!=` when checking the type `List[ChatMessage]`.
5+
This prevents false mismatches due to Python's `is` operator comparing object identity instead of equality.

0 commit comments

Comments
 (0)