Skip to content

Commit

Permalink
Merge pull request #473 from aurelio-labs/james/sort-keys-diff
Browse files Browse the repository at this point in the history
fix: sort func schemas and metadata in diffs
  • Loading branch information
jamescalam authored Dec 1, 2024
2 parents 77539cf + 89b3afe commit a7846a5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
14 changes: 13 additions & 1 deletion semantic_router/schema.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import datetime
from difflib import Differ
from enum import Enum
import json
import numpy as np
from typing import List, Optional, Union, Any, Dict, Tuple
from pydantic import BaseModel, Field
Expand Down Expand Up @@ -126,7 +127,18 @@ def to_tuple(self):

def to_str(self, include_metadata: bool = False):
if include_metadata:
return f"{self.route}: {self.utterance} | {self.function_schemas} | {self.metadata}"
# we sort the dicts to ensure consistent order as we need this to compare
# stringified function schemas accurately
if self.function_schemas is not None:
function_schemas_sorted: List[str] | None = [
json.dumps(schema, sort_keys=True)
for schema in self.function_schemas
]
else:
function_schemas_sorted = None
# we must do the same for metadata
metadata_sorted = json.dumps(self.metadata, sort_keys=True)
return f"{self.route}: {self.utterance} | {function_schemas_sorted} | {metadata_sorted}"
return f"{self.route}: {self.utterance}"

def to_diff_str(self, include_metadata: bool = False):
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ def test_utterance_diff(self, openai_encoder, routes, routes_2, index_cls):
if index_cls is PineconeIndex:
time.sleep(PINECONE_SLEEP) # allow for index to be populated
diff = route_layer_2.get_utterance_diff(include_metadata=True)
assert "+ Route 1: Hello | None | {'type': 'default'}" in diff
assert "+ Route 1: Hi | None | {'type': 'default'}" in diff
assert '+ Route 1: Hello | None | {"type": "default"}' in diff
assert '+ Route 1: Hi | None | {"type": "default"}' in diff
assert "- Route 1: Hello | None | {}" in diff
assert "+ Route 2: Au revoir | None | {}" in diff
assert "- Route 2: Hi | None | {}" in diff
Expand Down

0 comments on commit a7846a5

Please sign in to comment.