Why is my JSON object not inserting correctly in ChromaDB using Langchain and Python?

I am trying to enter a series of Topics into ChromaDB, those topics can be found here There are 35 total topics, 34 of which are unique. The topics are in valid JSON format – I can add and remove them from MongoDB, use JSON dumps and loads on them, and they were created from a langchain openai call where the output comes from a JSON formatter.

No matter what I try, however, only 7 of them are entered into ChromaDB AND trying to use a RecursiveJsonSplitter always results in an error.

Here are the two methods I am using (the write_object_to_prompt call simply removes all curly brackets and add in line breaks/indents):

def add_to_chroma(database_name: str, collection_name: str, json_object: json, user_directory: str, state, meta_data: json, doc_ids: list[str] = None):
    try:
        db_directory = os.path.join(user_directory, database_name + ".db")
        embedding_function = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
        # embedding_function = OpenAIEmbeddings(model="text-embedding-3-small")
        chroma_db = Chroma(persist_directory=db_directory, collection_name=collection_name, embedding_function=embedding_function,
                           collection_metadata={"hnsw:space": "cosine"})
        embed_object = write_object_to_prompt(json_object)
        text_splitter = RecursiveCharacterTextSplitter(chunk_size=2000, chunk_overlap=100)
        docs = text_splitter.create_documents(texts=[embed_object], metadatas=[meta_data])

        if doc_ids is None:
            doc_ids = [str(uuid.uuid4()) for i in range(1, len(docs) + 1)]
        else:
            # We look to see if the document exists:
            result = chroma_db.get(doc_ids)
            if result is not None and len(result) > 0:
                # This is an update:
                state["persistent_logs"].append("Updating " + meta_data["topic_id"] + " in Chroma")
                chroma_db.update_documents(doc_ids, docs)
                return doc_ids
        state["persistent_logs"].append("Adding " + meta_data["topic_id"] + " to Chroma")
        chroma_db.from_documents(docs, embedding_function, ids=doc_ids)
    except:
        trace_back = traceback.format_exc()
        logging.error("An unexpected error occurred attempting to add document to Chroma: " + database_name + ", to the collection: " + collection_name +
                      "nHere is the document that failed: " + write_object_to_prompt(json_object) + " nWith the error:n " + trace_back)
        state["persistent_logs"].append(
            "An unexpected error occurred attempting to add document to Chroma: " + database_name + ", to the collection: " + collection_name +
            "nHere is the document that failed: " + write_object_to_prompt(json_object) + " nWith the error:n " + trace_back)
    return doc_ids, state
def get_current_count(database_name: str, collection_name: str, user_directory: str) -> int:
    db_directory = os.path.join(user_directory, database_name + ".db")
    # embedding_function = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
    embedding_function = OpenAIEmbeddings(model="text-embedding-3-small")
    # Cosine will keep te similarity scores between zero and one
    chroma_db = Chroma(persist_directory=db_directory, collection_name=collection_name, embedding_function=embedding_function,
                       collection_metadata={"hnsw:space": "cosine"})
    results = chroma_db.get()
    total_count = 0
    if results is not None:
        total_count = len(results)
    return total_count

And here is my testing script:

user_directory = "../UserData/user-x"
with open("Topics.txt", "r") as f:
    Topics = json.load(f)
print("Total topics: " + str(len(Topics)))
state = {
        "errors": "",
        "persistent_logs": [],
    }
unique_ids = []
for this_topic in Topics:
    meta = {"topic_id": this_topic["topic_id"]}
    doc_ids, state = add_to_chroma("user-x", "conversations", this_topic, user_directory, state, meta)
    if this_topic["topic_id"] not in unique_ids:
        unique_ids.append(this_topic["topic_id"])
print("Number of unique Ids: " + str(len(unique_ids)))

if len(state["persistent_logs"]) > 0:
    for log in state["persistent_logs"]:
        print(log)
print("Total Topics in Chroma " + str(get_current_count("user-x", "topics", user_directory)))

I never get any errors back from Chroma and the output shows 35 entries.

I have tried to use OpenAi embeddings, HuggingFace embeddings, JSON and text splitters, creating ChomraDB Documents manually. Entering the JSON as plaintext, entering it as JSON. Nothing is working.

Does anybody have any ideas?

New contributor

Ken Tola is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật