Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

Fix duplicate Recording JOIN on combined search filters and 0.0-start speaker-sample drop, with regression tests

Murtaza Nasirc7c922518951
diff --git a/src/audio_chunking.py b/src/audio_chunking.py
index 995283ff..b5d9a569 100644
--- a/src/audio_chunking.py
+++ b/src/audio_chunking.py
@@ -1033,12 +1033,22 @@ def extract_speaker_samples(
# Handle both dict and object segments
if isinstance(seg, dict):
speaker = seg.get('speaker', 'Unknown')
- start = seg.get('start_time') or seg.get('start')
- end = seg.get('end_time') or seg.get('end')
+ # Use explicit None checks: a literal 0.0 start (segment at the very
+ # beginning) is valid and must not be treated as missing.
+ start = seg.get('start_time')
+ if start is None:
+ start = seg.get('start')
+ end = seg.get('end_time')
+ if end is None:
+ end = seg.get('end')
else:
speaker = getattr(seg, 'speaker', 'Unknown')
- start = getattr(seg, 'start_time', None) or getattr(seg, 'start', None)
- end = getattr(seg, 'end_time', None) or getattr(seg, 'end', None)
+ start = getattr(seg, 'start_time', None)
+ if start is None:
+ start = getattr(seg, 'start', None)
+ end = getattr(seg, 'end_time', None)
+ if end is None:
+ end = getattr(seg, 'end', None)
if speaker == 'Unknown' or start is None or end is None:
continue
diff --git a/src/services/embeddings.py b/src/services/embeddings.py
index 90f8b365..af23bc95 100644
--- a/src/services/embeddings.py
+++ b/src/services/embeddings.py
@@ -440,40 +440,38 @@ def basic_text_search_chunks(user_id, query, filters=None, top_k=5):
TranscriptChunk.recording_id.in_(accessible_recording_ids)
)
- # Apply filters if provided
+ # Apply filters if provided. The tag, speaker, and date filters all need
+ # a join to Recording; join it at most once so combining more than one of
+ # them does not raise a duplicate-JOIN / ambiguous-relationship error.
if filters:
+ if (filters.get('tag_ids') or filters.get('speaker_names')
+ or filters.get('date_from') or filters.get('date_to')):
+ chunks_query = chunks_query.join(Recording)
+
if filters.get('tag_ids'):
- chunks_query = chunks_query.join(Recording).join(
+ chunks_query = chunks_query.join(
RecordingTag, Recording.id == RecordingTag.recording_id
).filter(RecordingTag.tag_id.in_(filters['tag_ids']))
-
+
if filters.get('speaker_names'):
- # Filter by participants field in recordings instead of chunk speaker_name
- if not any(hasattr(desc, 'name') and desc.name == 'recording' for desc in chunks_query.column_descriptions):
- chunks_query = chunks_query.join(Recording)
-
- # Build OR conditions for each speaker name in participants
- speaker_conditions = []
- for speaker_name in filters['speaker_names']:
- speaker_conditions.append(
- Recording.participants.ilike(f'%{speaker_name}%')
- )
-
+ # Filter by the participants field on the recording.
+ speaker_conditions = [
+ Recording.participants.ilike(f'%{speaker_name}%')
+ for speaker_name in filters['speaker_names']
+ ]
chunks_query = chunks_query.filter(db.or_(*speaker_conditions))
current_app.logger.info(f"Applied speaker filter for: {filters['speaker_names']}")
-
+
if filters.get('recording_ids'):
chunks_query = chunks_query.filter(
TranscriptChunk.recording_id.in_(filters['recording_ids'])
)
-
- if filters.get('date_from') or filters.get('date_to'):
- chunks_query = chunks_query.join(Recording)
- if filters.get('date_from'):
- chunks_query = chunks_query.filter(Recording.meeting_date >= filters['date_from'])
- if filters.get('date_to'):
- chunks_query = chunks_query.filter(Recording.meeting_date <= filters['date_to'])
-
+
+ if filters.get('date_from'):
+ chunks_query = chunks_query.filter(Recording.meeting_date >= filters['date_from'])
+ if filters.get('date_to'):
+ chunks_query = chunks_query.filter(Recording.meeting_date <= filters['date_to'])
+
# Text search - filter stop words and rank by match count
stop_words = {'a', 'an', 'the', 'is', 'are', 'was', 'were', 'be', 'been',
'being', 'have', 'has', 'had', 'do', 'does', 'did', 'will',
@@ -574,41 +572,38 @@ def semantic_search_chunks(user_id, query, filters=None, top_k=5):
TranscriptChunk.recording_id.in_(accessible_recording_ids)
)
- # Apply filters if provided
+ # Apply filters if provided. The tag, speaker, and date filters all need
+ # a join to Recording; join it at most once so combining more than one of
+ # them does not raise a duplicate-JOIN / ambiguous-relationship error.
if filters:
+ if (filters.get('tag_ids') or filters.get('speaker_names')
+ or filters.get('date_from') or filters.get('date_to')):
+ chunks_query = chunks_query.join(Recording)
+
if filters.get('tag_ids'):
- # Join with recordings that have specified tags
- chunks_query = chunks_query.join(Recording).join(
+ chunks_query = chunks_query.join(
RecordingTag, Recording.id == RecordingTag.recording_id
).filter(RecordingTag.tag_id.in_(filters['tag_ids']))
-
+
if filters.get('speaker_names'):
- # Filter by participants field in recordings instead of chunk speaker_name
- if not any(hasattr(desc, 'name') and desc.name == 'recording' for desc in chunks_query.column_descriptions):
- chunks_query = chunks_query.join(Recording)
-
- # Build OR conditions for each speaker name in participants
- speaker_conditions = []
- for speaker_name in filters['speaker_names']:
- speaker_conditions.append(
- Recording.participants.ilike(f'%{speaker_name}%')
- )
-
+ # Filter by the participants field on the recording.
+ speaker_conditions = [
+ Recording.participants.ilike(f'%{speaker_name}%')
+ for speaker_name in filters['speaker_names']
+ ]
chunks_query = chunks_query.filter(db.or_(*speaker_conditions))
current_app.logger.info(f"Applied speaker filter for: {filters['speaker_names']}")
-
+
if filters.get('recording_ids'):
chunks_query = chunks_query.filter(
TranscriptChunk.recording_id.in_(filters['recording_ids'])
)
-
- if filters.get('date_from') or filters.get('date_to'):
- chunks_query = chunks_query.join(Recording)
- if filters.get('date_from'):
- chunks_query = chunks_query.filter(Recording.meeting_date >= filters['date_from'])
- if filters.get('date_to'):
- chunks_query = chunks_query.filter(Recording.meeting_date <= filters['date_to'])
-
+
+ if filters.get('date_from'):
+ chunks_query = chunks_query.filter(Recording.meeting_date >= filters['date_from'])
+ if filters.get('date_to'):
+ chunks_query = chunks_query.filter(Recording.meeting_date <= filters['date_to'])
+
# Get chunks that have embeddings
chunks = chunks_query.filter(TranscriptChunk.embedding.isnot(None)).all()
diff --git a/tests/test_cov_audio_chunking.py b/tests/test_cov_audio_chunking.py
index ce08209e..44a37493 100644
--- a/tests/test_cov_audio_chunking.py
+++ b/tests/test_cov_audio_chunking.py
@@ -922,3 +922,26 @@ def test_exception_classes():
raise ChunkProcessingError("x")
with pytest.raises(ChunkingNotSupportedError):
raise ChunkingNotSupportedError("y")
+
+
+def test_extract_speaker_samples_zero_start_not_dropped(tmp_path):
+ """Regression: a segment starting at exactly 0.0 must be kept. The code
+ previously read ``start_time or start``, so a literal 0.0 start was treated
+ as missing and the speaker whose only segment began at 0s was dropped."""
+ audio = tmp_path / "a.mp3"
+ audio.write_bytes(b"audio")
+ segments = [
+ {"speaker": "A", "start_time": 0.0, "end_time": 6.0},
+ {"speaker": "B", "start_time": 7.0, "end_time": 13.0},
+ ]
+
+ def fake_run(cmd, **kwargs):
+ with open(cmd[-1], "wb") as f:
+ f.write(b"sample")
+ return completed(returncode=0)
+
+ with mock.patch("subprocess.run", side_effect=fake_run), \
+ mock.patch.object(ac, "get_audio_duration_ffprobe", return_value=4.0):
+ out = extract_speaker_samples(str(audio), segments, str(tmp_path))
+ assert "A" in out # 0.0-start speaker no longer dropped
+ assert "B" in out
diff --git a/tests/test_cov_embeddings.py b/tests/test_cov_embeddings.py
index 8d856443..8a0adff4 100644
--- a/tests/test_cov_embeddings.py
+++ b/tests/test_cov_embeddings.py
@@ -940,3 +940,65 @@ def test_semantic_exception_returns_empty(api_emb):
with app.app_context():
with patch.object(api_emb, "_api_embed", side_effect=RuntimeError("boom")):
assert api_emb.semantic_search_chunks(1, "q") == []
+
+
+def test_semantic_combined_filters_no_duplicate_join(api_emb):
+ """Regression: combining tag + speaker + date filters previously appended a
+ second JOIN to Recording, raising an ambiguous-relationship / duplicate-JOIN
+ SQL error. They must now coexist."""
+ from datetime import datetime as _dt
+ from src.models import Tag, RecordingTag
+ with app.app_context():
+ user = _make_user()
+ rec = _make_recording(user, transcription="seed",
+ participants="Alice, Bob",
+ meeting_date=_dt(2026, 1, 15))
+ tag = Tag(name=f"cov-combo-sem-{os.getpid()}", user_id=user.id)
+ db.session.add(tag)
+ db.session.commit()
+ db.session.add(RecordingTag(recording_id=rec.id, tag_id=tag.id))
+ db.session.add(TranscriptChunk(
+ recording_id=rec.id, user_id=user.id, chunk_index=0, content="combined",
+ embedding=api_emb.serialize_embedding(np.array([1, 0, 0, 0], dtype=np.float32))))
+ db.session.commit()
+ query_vec = np.array([1, 0, 0, 0], dtype=np.float32)
+ with patch.object(api_emb, "_api_embed", return_value=[query_vec]):
+ results = api_emb.semantic_search_chunks(
+ user.id, "q",
+ filters={
+ "tag_ids": [tag.id],
+ "speaker_names": ["Alice"],
+ "date_from": _dt(2025, 1, 1),
+ "date_to": _dt(2027, 1, 1),
+ },
+ top_k=5)
+ assert len(results) == 1
+ assert results[0][0].content == "combined"
+
+
+def test_basic_combined_filters_no_duplicate_join(api_emb):
+ """Regression companion for basic_text_search_chunks: all filters at once."""
+ from datetime import datetime as _dt
+ from src.models import Tag, RecordingTag
+ with app.app_context():
+ user = _make_user()
+ rec = _make_recording(user, "the quick brown fox jumps",
+ participants="Alice, Bob",
+ meeting_date=_dt(2026, 1, 15))
+ tag = Tag(name=f"cov-combo-basic-{os.getpid()}", user_id=user.id)
+ db.session.add(tag)
+ db.session.commit()
+ db.session.add(RecordingTag(recording_id=rec.id, tag_id=tag.id))
+ db.session.commit()
+ _seed_chunks(api_emb, user, rec, ["the quick brown fox"])
+ results = api_emb.basic_text_search_chunks(
+ user.id, "quick fox",
+ filters={
+ "tag_ids": [tag.id],
+ "speaker_names": ["Alice"],
+ "date_from": _dt(2025, 1, 1),
+ "date_to": _dt(2027, 1, 1),
+ },
+ top_k=5)
+ assert isinstance(results, list)
+ assert len(results) >= 1