Skip to content

Hashkey Returns the Same Hash for Object with Child Objects #7

@0xF0CACC1A

Description

@0xF0CACC1A

The new OpenAI APIs require a new "messages" field in the body which contain an array of objects, however the current implementation produces the same hash key for the same amount of fields but with different values.

For example:

{
...
messages: [{"role": "user", "content": "message1"}]
}

Will produce the same hash as

{
...
messages: [{"role": "user", "content": "message2"}]
}

The following code should fix this issue but you may have to modify it to fit the environment. It goes through and recursively sorts any other objects found in the original object.

const sortJSON = function (json: any): any {
  if (Array.isArray(json)) {
    return json.map(sortJSON);
  } else if (typeof json === 'object' && json !== null) {
    json = Object.fromEntries(
      Object.entries(json)
        .filter(([_, value]) => value !== undefined && value !== null && value !== '')
    );
    return Object.keys(json)
      .sort()
      .reduce((acc, key) => {
        const value = json[key];
        if (value === undefined) {
          return acc;
        }
        return {
          ...acc,
          [key]: sortJSON(value),
        };
      }, {});
  } else {
    return json;
  }
}

export const getCacheKey = async function (props: any): Promise<string> {
  const propsWithoutUndefined = sortJSON(props)
  const hash = objectHash(propsWithoutUndefined);
  return hash;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions