Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

Merge main into fix/issue-994-embed-fallback-recursion-corrupt-output

Sameer6305a47954c19acb
diff --cc semantica/embeddings/methods.py
index d2e8848e,4128ffd8..907e8b6c
--- a/semantica/embeddings/methods.py
+++ b/semantica/embeddings/methods.py
@@@ -116,15 -117,15 +117,15 @@@ def generate_embeddings
>>> emb = generate_embeddings("Hello world", method="default")
>>> embs = generate_embeddings(["text1", "text2"], method="text")
"""
- # Check for custom method in registry
+ # Check for custom method in registry, skip self-reference
custom_method = method_registry.get("generation", method)
- if custom_method:
+ if custom_method and custom_method is not generate_embeddings:
- try:
- return custom_method(data, data_type=data_type, **kwargs)
- except Exception as e:
- logger.warning(
- f"Custom method {method} failed: {e}, falling back to default"
- )
+ fallback = kwargs.pop("fallback_on_custom_error", False)
+ result = call_custom_method(
+ logger, method, custom_method, data, data_type=data_type, fallback_on_custom_error=fallback, **kwargs
+ )
+ if result is not CUSTOM_METHOD_FELL_BACK:
+ return result
try:
if method == "default":
@@@ -164,15 -165,15 +165,15 @@@ def embed_text
>>> emb = embed_text("Hello world", method="sentence_transformers")
>>> embs = embed_text(["text1", "text2"], method="sentence_transformers")
"""
- # Check for custom method in registry
+ # Check for custom method in registry, skip self-reference
custom_method = method_registry.get("text", method)
- if custom_method:
+ if custom_method and custom_method is not embed_text:
- try:
- return custom_method(text, **kwargs)
- except Exception as e:
- logger.warning(
- f"Custom method {method} failed: {e}, falling back to default"
- )
+ fallback = kwargs.pop("fallback_on_custom_error", False)
+ result = call_custom_method(
+ logger, method, custom_method, text, fallback_on_custom_error=fallback, **kwargs
+ )
+ if result is not CUSTOM_METHOD_FELL_BACK:
+ return result
try:
# Get config
@@@ -224,15 -225,15 +225,15 @@@ def calculate_similarity
>>> similarity = calculate_similarity(emb1, emb2, method="cosine")
>>> print(f"Similarity: {similarity:.3f}")
"""
- # Check for custom method in registry
+ # Check for custom method in registry, skip self-reference
custom_method = method_registry.get("similarity", method)
- if custom_method:
+ if custom_method and custom_method is not calculate_similarity:
- try:
- return custom_method(embedding1, embedding2, **kwargs)
- except Exception as e:
- logger.warning(
- f"Custom method {method} failed: {e}, falling back to default"
- )
+ fallback = kwargs.pop("fallback_on_custom_error", False)
+ result = call_custom_method(
+ logger, method, custom_method, embedding1, embedding2, fallback_on_custom_error=fallback, **kwargs
+ )
+ if result is not CUSTOM_METHOD_FELL_BACK:
+ return result
try:
generator = EmbeddingGenerator(**kwargs)
@@@ -271,15 -272,15 +272,15 @@@ def pool_embeddings
>>> pooled = pool_embeddings(embeddings, method="mean")
>>> attention_pooled = pool_embeddings(embeddings, method="attention")
"""
- # Check for custom method in registry
+ # Check for custom method in registry, skip self-reference
custom_method = method_registry.get("pooling", method)
- if custom_method:
+ if custom_method and custom_method is not pool_embeddings:
- try:
- return custom_method(embeddings, **kwargs)
- except Exception as e:
- logger.warning(
- f"Custom method {method} failed: {e}, falling back to default"
- )
+ fallback = kwargs.pop("fallback_on_custom_error", False)
+ result = call_custom_method(
+ logger, method, custom_method, embeddings, fallback_on_custom_error=fallback, **kwargs
+ )
+ if result is not CUSTOM_METHOD_FELL_BACK:
+ return result
try:
strategy = PoolingStrategyFactory.create(method, **kwargs)