The following code is not working as intented, even though an error should be raised…
- User
The following code is not working as intented, even though an error should be raised when the content vector is null it skips it you know why? for _ in range(RETRY_COUNT): try: doc["content_vector"] = embeddings_engine.embed_query(doc["content"] ) break except: sleep(30) if doc["content_vector"] is None: raise Exception(f"Error getting embedding for chunk={chunk}")
- Assistant
If the
embed_queryfunction inembeddings_engineis supposed to raise an exception whendoc["content"]isNone, but it doesn't, there are a few potential issues that could be happening:-
Inside the
embed_querymethod, the case wheredoc["content"]isNonemight not be properly checked, and as a result, no exception is raised. -
If an exception is raised inside
embed_querybut it's caught and silenced within that function (for example, with atry-exceptblock that passes), you wouldn't see it in your outer code. -
If the
embed_queryfunction does not raise an exception but rather returnsNone, and this is considered normal behavior, then thedoc["content_vector"]would legitimately beNonewithout an exception being raised. -
If there is any other type of mishandling of
doc["content_vector"](like setting it toNoneinstead of raising an exception) inside theembed_querymethod, this could lead to no exception being raised outside.
The code snippet also uses a bare
except:without specifying the exception type, which is not recommended because it will catch literally any exception, including system interrupts, which can make it harder to debug problems. It's better to catch specific exceptions that you expect could occur.Also, an exception should be raised if
doc["content_vector"] is Noneafter all retries have been exhausted. However, it is important to note that theifblock that raises the exception is outside theforloop – ensure there's no code between the end of theforloop and theifcheck that could alterdoc["content_vector"].Otherwise,doc["content_vector"]might be set toNoneafter theforloop, and before theifcheck, accidentally.Here's a revised version of your code with a specified exception type and an added print statement to debug:
RETRY_COUNT = 3 for _ in range(RETRY_COUNT): try: doc["content_vector"] = embeddings_engine.embed_query(doc["content"]) if doc["content_vector"] is not None: break except SpecificEmbeddingException as e: print(f"An error occurred: {e}") sleep(30) if doc["content_vector"] is None: raise Exception(f"Error getting embedding for chunk={chunk}")In this revised code, replace
SpecificEmbeddingExceptionwith the actual exception type that you would expect from yourembed_querymethod if it fails. If the method might returnNonenormally, then handle that case as shown above, and not through an exception.Finally, make sure that the
embeddings_engine.embed_query()method is designed to raise an exception whendoc["content"]isNoneif that is the behavior you're relying on. Otherwise, you would need to explicitly check forNonebefore calling the function and handle the situation according to your application's logic. -
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.