fix(api): bound ChildChunk content with max_length to cap embedding cost - #40940
Open
Harsh23Kashyap wants to merge 2 commits into
Open
fix(api): bound ChildChunk content with max_length to cap embedding cost#40940Harsh23Kashyap wants to merge 2 commits into
Harsh23Kashyap wants to merge 2 commits into
Conversation
ChildChunkCreatePayload.content and ChildChunkUpdatePayload.content (api/controllers/common/controller_schemas.py) accepted arbitrary-length strings. A service-API caller could submit a multi-MB content value that was then forwarded to the embedding model and persisted in child_chunks.content (LongText, unbounded), with the cost landing on the tenant owner. Sibling of langgenius#39825 (TTS text has no max_length). Adds a CHILD_CHUNK_CONTENT_MAX_LENGTH = 16384 cap (4x the default child chunk size of ~4096 chars, well above the existing 10000-char "very long content" test) and two boundary tests per payload: 16384 chars accepted, 16385 raises ValidationError. Fixes langgenius#40825
Harsh23Kashyap
requested review from
QuantumGhost and
laipz8200
as code owners
August 18, 2026 13:00
Contributor
Pyrefly Type Coverage
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Bound
ChildChunkCreatePayload.contentandChildChunkUpdatePayload.contentwith amax_length=16384cap to prevent unbounded content from being forwarded to the embedding model and persisted tochild_chunks.content(LongText, unbounded).Sibling of #39825 (TTS text had no
max_length). Same pattern, different surface: a service-API caller can no longer submit a multi-MB child chunk and burn tenant-owned embedding budget.Problem
The two Pydantic models in
api/controllers/common/controller_schemas.pyaccepted arbitrary-length strings. APOSTto:POST /v1/datasets/{dataset_id}/documents/{document_id}/segments/{segment_id}/child_chunks(service API)POST /console/api/datasets/{dataset_id}/documents/{document_id}/segments/{segment_id}/child_chunks(console)…could carry a
contentvalue of any size, and the value was then:ChildChunkAddToIndexTask/ChildChunkUpdateTask(cost proportional to token count, billed to the tenant owner).child_chunks.content(typed asLongText, unbounded), skewing row size, slowing indexes, and bloating backups.Fix
Added
CHILD_CHUNK_CONTENT_MAX_LENGTH = 16384as a module-level constant and applied it viamax_length=...on bothChildChunkCreatePayload.contentandChildChunkUpdatePayload.content.The cap sits well above the default child chunk size (~1024 tokens ≈ 4096 chars at ~4 chars/token) and above the existing 10000-char "very long content" test (
tests/unit_tests/controllers/service_api/dataset/test_dataset_segment.py:222-226), while still bounding abuse.Tests
Extended the existing
TestChildChunkCreatePayloadandTestChildChunkUpdatePayloadclasses intests/unit_tests/controllers/service_api/dataset/test_dataset_segment.pywith one boundary test each per direction:test_payload_accepts_content_at_max_length— content of lengthCHILD_CHUNK_CONTENT_MAX_LENGTHis accepted.test_payload_rejects_content_above_max_length— content of lengthCHILD_CHUNK_CONTENT_MAX_LENGTH + 1raisesValidationError.The existing 10000-char "very long content" test continues to pass because 10000 < 16384.
Files changed
api/controllers/common/controller_schemas.py— 1 new constant, 2 fields updated withmax_length.api/tests/unit_tests/controllers/service_api/dataset/test_dataset_segment.py— 4 new test methods (2 per payload class), 1 import.Verification
pytest tests/unit_tests/controllers/service_api/dataset/test_dataset_segment.py::TestChildChunkCreatePayload tests/unit_tests/controllers/service_api/dataset/test_dataset_segment.py::TestChildChunkUpdatePayload --noconftest -v→ 11/11 pass (5 + 2 existing, 2 + 2 new).ruff checkon both files → all checks passed.ruff format --checkon both files → already formatted.pyrefly checkonapi/controllers/common/controller_schemas.py→ 0 diagnostics.Scope
Out of scope (potential follow-ups)
MessageFeedbackPayload.content(api/controllers/common/controller_schemas.py:94) — same pattern (nomax_length).ConversationRenamePayload.name(api/controllers/common/controller_schemas.py:13) — same pattern.MetadataUpdatePayload.name(api/controllers/common/controller_schemas.py:212) — same pattern.ChildChunklives under a parentSegment).core/rag/index_processor/.Fixes #40825