microsoft/semantic-kernel · error · ServiceResponseException

Could not upsert messages.

Error message

Could not upsert messages.

What it means

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.

Source

Thrown at python/semantic_kernel/connectors/memory_stores/redis/redis_memory_store.py:221

        Returns:
            str: Redis key associated with the upserted memory record
        """
        if not await self.does_collection_exist(collection_name):
            raise ServiceResourceNotFoundError(f'Collection "{collection_name}" does not exist')

        # Typical Redis key structure: collection_name:{some identifier}
        record._key = get_redis_key(collection_name, record._id)

        # Overwrites previous data or inserts new key if not present
        # Index registers any hash matching its schema and prefixed with collection_name:
        try:
            self._database.hset(
                record._key,
                mapping=serialize_record_to_redis(record, self._vector_type),
            )
            return record._key
        except Exception as e:
            raise ServiceResponseException("Could not upsert messages.") from e

    async def upsert_batch(self, collection_name: str, records: list[MemoryRecord]) -> list[str]:
        """Upserts a group of memory records into the data store.

        Does not guarantee that the collection exists.
            * If the record already exists, it will be updated.
            * If the record does not exist, it will be created.

        Note: if the records do not have the same dimensionality configured for the collection,
        they will not be detected to belong to the collection in Redis.

        Args:
            collection_name (str): Name for a collection of embeddings
            records (List[MemoryRecord]): List of memory records to upsert

        Returns:
            List[str]: Redis keys associated with the upserted memory records
        """

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Inspect the chained exception (e) for the exact Redis error (e.g. WRONGTYPE, OOM).
  2. If WRONGTYPE, delete the offending key or use a different collection so the key prefix is clean.
  3. Retry with backoff for transient connection/OOM errors.
  4. Verify self._vector_type matches the record embedding dtype before serializing.

Example fix

// before
key = await store.upsert('mycol', record)
// after
try:
    key = await store.upsert('mycol', record)
except ServiceResponseException as e:
    raise RuntimeError(f'upsert failed: {e.__cause__}') from e
Defensive patterns

Strategy: try-catch

Try / catch

try:
    key = await store.upsert(collection_name, record)
except ServiceResponseException as e:
    cause = e.__cause__
    # WRONGTYPE -> clean key; OOM/connection -> retry

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/1f838d47362e0bbd. Report an issue: GitHub.