{"record":{"id":"1f838d47362e0bbd","repo":"microsoft/semantic-kernel","slug":"could-not-upsert-messages","errorCode":null,"errorMessage":"Could not upsert messages.","messagePattern":"Could not upsert messages\\.","errorType":"exception","errorClass":"ServiceResponseException","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/connectors/memory_stores/redis/redis_memory_store.py","lineNumber":221,"sourceCode":"        Returns:\n            str: Redis key associated with the upserted memory record\n        \"\"\"\n        if not await self.does_collection_exist(collection_name):\n            raise ServiceResourceNotFoundError(f'Collection \"{collection_name}\" does not exist')\n\n        # Typical Redis key structure: collection_name:{some identifier}\n        record._key = get_redis_key(collection_name, record._id)\n\n        # Overwrites previous data or inserts new key if not present\n        # Index registers any hash matching its schema and prefixed with collection_name:\n        try:\n            self._database.hset(\n                record._key,\n                mapping=serialize_record_to_redis(record, self._vector_type),\n            )\n            return record._key\n        except Exception as e:\n            raise ServiceResponseException(\"Could not upsert messages.\") from e\n\n    async def upsert_batch(self, collection_name: str, records: list[MemoryRecord]) -> list[str]:\n        \"\"\"Upserts a group of memory records into the data store.\n\n        Does not guarantee that the collection exists.\n            * If the record already exists, it will be updated.\n            * If the record does not exist, it will be created.\n\n        Note: if the records do not have the same dimensionality configured for the collection,\n        they will not be detected to belong to the collection in Redis.\n\n        Args:\n            collection_name (str): Name for a collection of embeddings\n            records (List[MemoryRecord]): List of memory records to upsert\n\n        Returns:\n            List[str]: Redis keys associated with the upserted memory records\n        \"\"\"","sourceCodeStart":203,"sourceCodeEnd":239,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/memory_stores/redis/redis_memory_store.py#L203-L239","documentation":"Raised by RedisMemoryStore.upsert() when self._database.hset(...) throws any Exception. ServiceResponseException with literal message 'Could not upsert messages.' and the original chained via `from e`. The collection-existence check has already passed; this is a write-time failure on the hash.","triggerScenarios":"Calling upsert() on an existing index where hset fails: connection drop mid-write, wrong data type already stored at the key (e.g. a string where a hash is expected), memory pressure / OOM, or serialize_record_to_redis producing an invalid mapping.","commonSituations":"Key collision where a non-hash value was written out-of-band; Redis maxmemory eviction policy rejecting writes; transient network reset; serialization of an embedding with the wrong numpy dtype.","solutions":["Inspect the chained exception (e) for the exact Redis error (e.g. WRONGTYPE, OOM).","If WRONGTYPE, delete the offending key or use a different collection so the key prefix is clean.","Retry with backoff for transient connection/OOM errors.","Verify self._vector_type matches the record embedding dtype before serializing."],"exampleFix":"// before\nkey = await store.upsert('mycol', record)\n// after\ntry:\n    key = await store.upsert('mycol', record)\nexcept ServiceResponseException as e:\n    raise RuntimeError(f'upsert failed: {e.__cause__}') from e","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    key = await store.upsert(collection_name, record)\nexcept ServiceResponseException as e:\n    cause = e.__cause__\n    # WRONGTYPE -> clean key; OOM/connection -> retry","preventionTips":["Avoid writing non-hash values to keys under the collection prefix out-of-band (causes WRONGTYPE).","Match self._vector_type to the embedding dtype before serializing.","Retry transient OOM/connection errors with backoff; inspect the chained cause."],"tags":["redis","memory-store","upsert","service-response-exception","write-failure"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}