Skip to content

Commit 67fb9aa

Browse files
committed
Apply systematic pattern fixes: prevent metadata conflicts and race conditions
- Fix TRY003: Remove long inline message from AzureConfigError exception - Fix stale metadata: Clear translation_error on successful fallback translation - Fix docstring accuracy: Correct 'language_metadata' to 'language' key - Add noop provider short-circuit to avoid unnecessary translation attempts - Fix provider cache race condition using setdefault for thread safety - Apply consistent patterns across primary and fallback provider caching
1 parent ce343c2 commit 67fb9aa

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

cognee/tasks/translation/translate_content.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,8 @@ async def _translate_and_update(ctx: TranslationContext) -> None:
344344
confidence_score=translation_confidence or 0.0,
345345
)
346346
ctx.chunk.metadata["translation"] = trans.model_dump()
347+
# Clear any prior failure marker once we have a success
348+
ctx.chunk.metadata.pop("translation_error", None)
347349

348350

349351
# Test helpers for registry isolation
@@ -592,7 +594,7 @@ def __init__(self):
592594
region = os.getenv("AZURE_TRANSLATE_REGION") # optional; required for some resources
593595

594596
if not key:
595-
raise AzureConfigError("AZURE_TRANSLATE_KEY is required (and AZURE_TRANSLATE_ENDPOINT/REGION as applicable).")
597+
raise AzureConfigError()
596598
if not endpoint:
597599
# Default to global Translator endpoint when not explicitly provided
598600
endpoint = "https://api.cognitive.microsofttranslator.com"
@@ -713,7 +715,7 @@ async def _process_chunk(chunk, plan, provider_cache):
713715
provider = provider_cache.get(primary_key)
714716
if provider is None:
715717
provider = _get_provider(primary_key)
716-
provider_cache[primary_key] = provider
718+
provider = provider_cache.setdefault(primary_key, provider)
717719
except Exception as e:
718720
if isinstance(e, asyncio.CancelledError):
719721
raise
@@ -740,6 +742,9 @@ async def _process_chunk(chunk, plan, provider_cache):
740742
_attach_language_metadata(ctx)
741743

742744
if ctx.requires_translation:
745+
# Short-circuit: primary provider cannot translate and no fallbacks provided
746+
if primary_key == "noop" and not fallback_providers:
747+
return ctx.chunk
743748
await _translate_and_update(ctx)
744749
# If no translation metadata was produced, try fallbacks in order
745750
if "translation" not in getattr(ctx.chunk, "metadata", {}):
@@ -748,7 +753,7 @@ async def _process_chunk(chunk, plan, provider_cache):
748753
alt_provider = provider_cache.get(alt_name)
749754
if alt_provider is None:
750755
alt_provider = _get_provider(alt_name)
751-
provider_cache[alt_name] = alt_provider
756+
alt_provider = provider_cache.setdefault(alt_name, alt_provider)
752757
except Exception as e:
753758
if isinstance(e, asyncio.CancelledError):
754759
raise
@@ -794,7 +799,7 @@ async def translate_content(*chunks: Any, **kwargs) -> Any:
794799
Any: For single chunk input - returns the processed chunk directly.
795800
For multiple chunks input - returns List[Any] of processed chunks.
796801
Each returned chunk may have its text translated and metadata updated with:
797-
- language_metadata: detected language and confidence
802+
- language: detected language and confidence
798803
- translation: translated text and provider information (if translation occurred)
799804
"""
800805
# Always work with a list internally for consistency

0 commit comments

Comments
 (0)