{"id":39199,"date":"2025-11-08T02:47:16","date_gmt":"2025-11-08T10:47:16","guid":{"rendered":"https:\/\/cloudinary.com\/blog\/?p=39199"},"modified":"2025-11-08T02:47:17","modified_gmt":"2025-11-08T10:47:17","slug":"how-to-handle-a-valueerror-in-python","status":"publish","type":"post","link":"https:\/\/cloudinary.com\/blog\/questions\/how-to-handle-a-valueerror-in-python\/","title":{"rendered":"How to handle a ValueError in Python"},"content":{"rendered":"\n<p>ValueError is one of those Python exceptions you will encounter early and often. It usually pops up when a function receives the right type of argument but an invalid value. Think parsing user input, converting strings to numbers, or validating data coming from APIs and forms.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Question:<\/h2>\n\n\n\n<p><em>Hi all,<\/em><br><em>I keep running into ValueError when parsing user input and external data in my Python app. Examples include int(&#8220;abc&#8221;), converting dates, and validating parameters for image processing tasks. I want to catch and handle ValueError without masking real bugs and also ensure I return helpful error messages to API clients.<\/em><br><br><em>Specifically: How to handle a ValueError in Python in a clean, maintainable way? What are practical patterns for validation, exception chaining, logging, and testing? Any examples for parameter validation when building URLs or doing media processing would be great.<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Answer:<\/h2>\n\n\n\n<p>A <code>ValueError<\/code> means that whatever operation you\u2019re trying to do (converting a string to an integer, trying to add an integer to a string, passing a value to an argument) isn\u2019t using the right data type. This issue can be pretty common for newer developers, but even seasoned devs can run into this thanks to Python\u2019s lack of static data typing. Thankfully, it can be a pretty easy fix\u2013granted that you catch it early.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Common ways ValueError occurs<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Type conversions that fail, like trying to convert a string to an integer<\/li>\n\n\n\n<li>Parsing dates, UUIDs, or enums<\/li>\n\n\n\n<li>Out-of-range values such as negative sizes, zero division parameters, invalid indices<\/li>\n\n\n\n<li>Misconfigured settings passed as strings from env or config files<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Core handling pattern<\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\">def parse_positive_int(value, name=<span class=\"hljs-string\">\"value\"<\/span>):\n\u00a0 \u00a0 <span class=\"hljs-keyword\">try<\/span>:\n\u00a0 \u00a0 \u00a0 \u00a0 num = int(value)\n\u00a0 \u00a0 except ValueError <span class=\"hljs-keyword\">as<\/span> e:\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-comment\"># Preserve context with exception chaining<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 raise ValueError(f<span class=\"hljs-string\">\"{name} must be an integer. Got: {value!r}\"<\/span>) from e\n\n\u00a0 \u00a0 <span class=\"hljs-keyword\">if<\/span> num &lt;= <span class=\"hljs-number\">0<\/span>:\n\u00a0 \u00a0 \u00a0 \u00a0 raise ValueError(f<span class=\"hljs-string\">\"{name} must be &gt; 0. Got: {num}\"<\/span>)\n\n\u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> num\n\ndef handler(data):\n\u00a0 \u00a0 <span class=\"hljs-comment\"># Validate early, fail fast<\/span>\n\u00a0 \u00a0 width = parse_positive_int(data.get(<span class=\"hljs-string\">\"width\"<\/span>), name=<span class=\"hljs-string\">\"width\"<\/span>)\n\u00a0 \u00a0 height = parse_positive_int(data.get(<span class=\"hljs-string\">\"height\"<\/span>), name=<span class=\"hljs-string\">\"height\"<\/span>)\n\u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> {<span class=\"hljs-string\">\"w\"<\/span>: width, <span class=\"hljs-string\">\"h\"<\/span>: height}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Try-except with else-finally<\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\">import logging\n\ndef process_payload(payload):\n\u00a0 \u00a0 <span class=\"hljs-keyword\">try<\/span>:\n\u00a0 \u00a0 \u00a0 \u00a0 size = parse_positive_int(payload&#91;<span class=\"hljs-string\">\"size\"<\/span>], <span class=\"hljs-string\">\"size\"<\/span>)\n\u00a0 \u00a0 except KeyError:\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-comment\"># Wrong exception type for missing keys<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 raise ValueError(<span class=\"hljs-string\">\"Missing 'size' field\"<\/span>)\n\u00a0 \u00a0 except ValueError <span class=\"hljs-keyword\">as<\/span> e:\n\u00a0 \u00a0 \u00a0 \u00a0 logging.warning(<span class=\"hljs-string\">\"Invalid 'size' in payload: %s\"<\/span>, e)\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-comment\"># Re-raise or return a user-facing message<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 raise\n\u00a0 \u00a0 <span class=\"hljs-keyword\">else<\/span>:\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-comment\"># Runs only if no exception occurred<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> {<span class=\"hljs-string\">\"ok\"<\/span>: <span class=\"hljs-keyword\">True<\/span>, <span class=\"hljs-string\">\"size\"<\/span>: size}\n\u00a0 \u00a0 <span class=\"hljs-keyword\">finally<\/span>:\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-comment\"># Cleanup if needed<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 logging.debug(<span class=\"hljs-string\">\"process_payload finished\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Best practices<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Validate inputs at boundaries. <\/strong>Check ranges, allowed sets, and formats before deeper logic.<\/li>\n\n\n\n<li><strong>Use clear error messages. <\/strong>Include the parameter name and offending value.<\/li>\n\n\n\n<li><strong>Preserve context with <\/strong><code>raise ... from e<\/code><strong>.<\/strong> This keeps the original traceback visible.<\/li>\n\n\n\n<li><strong>Log at the right level. <\/strong>Use <code>logging.exception<\/code> in unexpected paths you plan to investigate.<\/li>\n\n\n\n<li><strong>Convert to domain errors at boundaries.<\/strong> For web apps, map <code>ValueError<\/code> to a 400 response<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Unit testing ValueError<\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">import<\/span> pytest\n\ndef test_parse_positive_int_rejects_non_int():\n\u00a0 \u00a0 <span class=\"hljs-keyword\">with<\/span> pytest.raises(ValueError):\n\u00a0 \u00a0 \u00a0 \u00a0 parse_positive_int(<span class=\"hljs-string\">\"abc\"<\/span>, <span class=\"hljs-string\">\"width\"<\/span>)\n\ndef test_parse_positive_int_rejects_non_positive():\n\u00a0 \u00a0 <span class=\"hljs-keyword\">with<\/span> pytest.raises(ValueError):\n\u00a0 \u00a0 \u00a0 \u00a0 parse_positive_int(<span class=\"hljs-string\">\"-10\"<\/span>, <span class=\"hljs-string\">\"width\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Guarding conversions and I-O<\/h3>\n\n\n\n<p>When you are saving or transforming images in Python, it is common to validate file paths, sizes, and formats before acting. If you are dealing with local processing and file writes, review patterns like buffered writes and atomic saves to avoid partial outputs. See approaches in <a href=\"https:\/\/cloudinary.com\/guides\/web-performance\/6-ways-to-save-images-in-python\">6 ways to save images in Python<\/a> and validate before writing.<\/p>\n\n\n\n<p>For string encodings and conversions, especially around request payloads and caching, Base64 handling is a frequent source of ValueError. Validate input length and characters before decoding. For deeper context, see this overview of <a href=\"https:\/\/cloudinary.com\/guides\/image-formats\/image-conversion-to-base64-in-python-a-comprehensive-guide\">image conversion to Base64 in Python<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mapping ValueError to user-friendly API responses<\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\"><span class=\"hljs-comment\"># Example with FastAPI<\/span>\nfrom fastapi import FastAPI, HTTPException\n\napp = FastAPI()\n\n@app.post(<span class=\"hljs-string\">\"\/resize\"<\/span>)\ndef resize(payload: dict):\n\u00a0 \u00a0 <span class=\"hljs-keyword\">try<\/span>:\n\u00a0 \u00a0 \u00a0 \u00a0 w = parse_positive_int(payload.get(<span class=\"hljs-string\">\"w\"<\/span>), <span class=\"hljs-string\">\"w\"<\/span>)\n\u00a0 \u00a0 \u00a0 \u00a0 h = parse_positive_int(payload.get(<span class=\"hljs-string\">\"h\"<\/span>), <span class=\"hljs-string\">\"h\"<\/span>)\n\u00a0 \u00a0 except ValueError <span class=\"hljs-keyword\">as<\/span> e:\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-comment\"># Translate to a 400<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 raise HTTPException(status_code=<span class=\"hljs-number\">400<\/span>, detail=str(e))\n\u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> {<span class=\"hljs-string\">\"w\"<\/span>: w, <span class=\"hljs-string\">\"h\"<\/span>: h}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Enhancing this with Cloudinary<\/h3>\n\n\n\n<p>If you generate or deliver images via URLs, keep parameter validation strict to avoid malformed links and wasted requests. For example, validate width, height, and format before building an <a href=\"https:\/\/cloudinary.com\/glossary\/image-url\">image URL<\/a>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\"><span class=\"hljs-comment\"># Example using Cloudinary's Python SDK<\/span>\nimport cloudinary\nimport cloudinary.uploader\n\ncloudinary.config(cloud_name=<span class=\"hljs-string\">\"demo\"<\/span>, api_key=<span class=\"hljs-string\">\"key\"<\/span>, api_secret=<span class=\"hljs-string\">\"secret\"<\/span>)\n\nALLOWED_FORMATS = {<span class=\"hljs-string\">\"jpg\"<\/span>, <span class=\"hljs-string\">\"png\"<\/span>, <span class=\"hljs-string\">\"webp\"<\/span>}\n\ndef upload_with_transform(path, width, fmt):\n\u00a0 \u00a0 <span class=\"hljs-comment\"># Validate before calling SDK<\/span>\n\u00a0 \u00a0 width = parse_positive_int(width, <span class=\"hljs-string\">\"width\"<\/span>)\n\u00a0 \u00a0 <span class=\"hljs-keyword\">if<\/span> fmt not in ALLOWED_FORMATS:\n\u00a0 \u00a0 \u00a0 \u00a0 raise ValueError(f<span class=\"hljs-string\">\"fmt must be one of {sorted(ALLOWED_FORMATS)}. Got: {fmt!r}\"<\/span>)\n\n\u00a0 \u00a0 <span class=\"hljs-keyword\">try<\/span>:\n\u00a0 \u00a0 \u00a0 \u00a0 res = cloudinary.uploader.upload(\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 path,\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 transformation=&#91;{<span class=\"hljs-string\">\"width\"<\/span>: width, <span class=\"hljs-string\">\"crop\"<\/span>: <span class=\"hljs-string\">\"scale\"<\/span>, <span class=\"hljs-string\">\"fetch_format\"<\/span>: fmt}]\n\u00a0 \u00a0 \u00a0 \u00a0 )\n\u00a0 \u00a0 except <span class=\"hljs-keyword\">Exception<\/span> <span class=\"hljs-keyword\">as<\/span> e:\n\u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-comment\"># Wrap unexpected SDK or network errors<\/span>\n\u00a0 \u00a0 \u00a0 \u00a0 raise RuntimeError(<span class=\"hljs-string\">\"Upload or transform failed\"<\/span>) from e\n\n\u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> res&#91;<span class=\"hljs-string\">\"secure_url\"<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This pattern validates early, raises helpful <code>ValueError<\/code> messages for clients, and cleanly distinguishes validation errors from operational failures.\u00a0<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">TL;DR<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Validate inputs at boundaries and raise <code>ValueError<\/code> with clear messages.<\/li>\n\n\n\n<li>Use try-except with exception chaining to preserve context.<\/li>\n\n\n\n<li>Log unexpected paths and convert <code>ValueError<\/code> to 400 responses in APIs.<\/li>\n\n\n\n<li>When building media URLs or uploads, validate size and format before calling external services for cleaner failures and fewer retries.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Learn More<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/cloudinary.com\/tools\/png-to-webp\">PNG to WebP Converter<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/cloudinary.com\/tools\/image-upscale\">Image Upscaling and Quality Enhancement<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/cloudinary.com\/tools\/image-to-jpg\">Image to JPG Converter<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/cloudinary.com\/tools\/webm-converter\">WebM Converter<\/a><\/li>\n<\/ul>\n\n\n\n<p><a href=\"https:\/\/cloudinary.com\/users\/register_free\">Create your free Cloudinary account<\/a> and streamline your image and video workflow from upload to delivery.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ValueError is one of those Python exceptions you will encounter early and often. It usually pops up when a function receives the right type of argument but an invalid value. Think parsing user input, converting strings to numbers, or validating data coming from APIs and forms. Question: Hi all,I keep running into ValueError when parsing [&hellip;]<\/p>\n","protected":false},"author":88,"featured_media":39200,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_cloudinary_featured_overwrite":false,"footnotes":""},"categories":[1],"tags":[423],"class_list":["post-39199","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-questions"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v25.6 (Yoast SEO v26.9) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>How to handle a ValueError in Python<\/title>\n<meta name=\"description\" content=\"ValueError is one of those Python exceptions you will encounter early and often. It usually pops up when a function receives the right type of argument\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/cloudinary.com\/blog\/questions\/how-to-handle-a-valueerror-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to handle a ValueError in Python\" \/>\n<meta property=\"og:description\" content=\"ValueError is one of those Python exceptions you will encounter early and often. It usually pops up when a function receives the right type of argument\" \/>\n<meta property=\"og:url\" content=\"https:\/\/cloudinary.com\/blog\/questions\/how-to-handle-a-valueerror-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Cloudinary Blog\" \/>\n<meta property=\"article:published_time\" content=\"2025-11-08T10:47:16+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-11-08T10:47:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1762598817\/how_to_handle_a_value_error_in_python_featured_image\/how_to_handle_a_value_error_in_python_featured_image.jpg?_i=AA\" \/>\n\t<meta property=\"og:image:width\" content=\"2000\" \/>\n\t<meta property=\"og:image:height\" content=\"1100\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"damjanantevski\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"NewsArticle\",\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/how-to-handle-a-valueerror-in-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/how-to-handle-a-valueerror-in-python\/\"},\"author\":{\"name\":\"damjanantevski\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#\/schema\/person\/43592e43c12520a1e867d456b1e8cf7e\"},\"headline\":\"How to handle a ValueError in Python\",\"datePublished\":\"2025-11-08T10:47:16+00:00\",\"dateModified\":\"2025-11-08T10:47:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/how-to-handle-a-valueerror-in-python\/\"},\"wordCount\":575,\"publisher\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/how-to-handle-a-valueerror-in-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1762598817\/how_to_handle_a_value_error_in_python_featured_image\/how_to_handle_a_value_error_in_python_featured_image.jpg?_i=AA\",\"keywords\":[\"Questions\"],\"inLanguage\":\"en-US\",\"copyrightYear\":\"2025\",\"copyrightHolder\":{\"@id\":\"https:\/\/cloudinary.com\/#organization\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/how-to-handle-a-valueerror-in-python\/\",\"url\":\"https:\/\/cloudinary.com\/blog\/questions\/how-to-handle-a-valueerror-in-python\/\",\"name\":\"How to handle a ValueError in Python\",\"isPartOf\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/how-to-handle-a-valueerror-in-python\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/how-to-handle-a-valueerror-in-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1762598817\/how_to_handle_a_value_error_in_python_featured_image\/how_to_handle_a_value_error_in_python_featured_image.jpg?_i=AA\",\"datePublished\":\"2025-11-08T10:47:16+00:00\",\"dateModified\":\"2025-11-08T10:47:17+00:00\",\"description\":\"ValueError is one of those Python exceptions you will encounter early and often. It usually pops up when a function receives the right type of argument\",\"breadcrumb\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/how-to-handle-a-valueerror-in-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/cloudinary.com\/blog\/questions\/how-to-handle-a-valueerror-in-python\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/how-to-handle-a-valueerror-in-python\/#primaryimage\",\"url\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1762598817\/how_to_handle_a_value_error_in_python_featured_image\/how_to_handle_a_value_error_in_python_featured_image.jpg?_i=AA\",\"contentUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1762598817\/how_to_handle_a_value_error_in_python_featured_image\/how_to_handle_a_value_error_in_python_featured_image.jpg?_i=AA\",\"width\":2000,\"height\":1100},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/cloudinary.com\/blog\/questions\/how-to-handle-a-valueerror-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/cloudinary.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to handle a ValueError in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#website\",\"url\":\"https:\/\/cloudinary.com\/blog\/\",\"name\":\"Cloudinary Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/cloudinary.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#organization\",\"name\":\"Cloudinary Blog\",\"url\":\"https:\/\/cloudinary.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA\",\"contentUrl\":\"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA\",\"width\":312,\"height\":60,\"caption\":\"Cloudinary Blog\"},\"image\":{\"@id\":\"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#\/schema\/person\/43592e43c12520a1e867d456b1e8cf7e\",\"name\":\"damjanantevski\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cloudinary.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/3b40c995531fe4d510212a06c9d4fc666d2cb8efbfebc98a94191701accf4817?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/3b40c995531fe4d510212a06c9d4fc666d2cb8efbfebc98a94191701accf4817?s=96&d=mm&r=g\",\"caption\":\"damjanantevski\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to handle a ValueError in Python","description":"ValueError is one of those Python exceptions you will encounter early and often. It usually pops up when a function receives the right type of argument","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/cloudinary.com\/blog\/questions\/how-to-handle-a-valueerror-in-python\/","og_locale":"en_US","og_type":"article","og_title":"How to handle a ValueError in Python","og_description":"ValueError is one of those Python exceptions you will encounter early and often. It usually pops up when a function receives the right type of argument","og_url":"https:\/\/cloudinary.com\/blog\/questions\/how-to-handle-a-valueerror-in-python\/","og_site_name":"Cloudinary Blog","article_published_time":"2025-11-08T10:47:16+00:00","article_modified_time":"2025-11-08T10:47:17+00:00","og_image":[{"width":2000,"height":1100,"url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1762598817\/how_to_handle_a_value_error_in_python_featured_image\/how_to_handle_a_value_error_in_python_featured_image.jpg?_i=AA","type":"image\/jpeg"}],"author":"damjanantevski","twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"NewsArticle","@id":"https:\/\/cloudinary.com\/blog\/questions\/how-to-handle-a-valueerror-in-python\/#article","isPartOf":{"@id":"https:\/\/cloudinary.com\/blog\/questions\/how-to-handle-a-valueerror-in-python\/"},"author":{"name":"damjanantevski","@id":"https:\/\/cloudinary.com\/blog\/#\/schema\/person\/43592e43c12520a1e867d456b1e8cf7e"},"headline":"How to handle a ValueError in Python","datePublished":"2025-11-08T10:47:16+00:00","dateModified":"2025-11-08T10:47:17+00:00","mainEntityOfPage":{"@id":"https:\/\/cloudinary.com\/blog\/questions\/how-to-handle-a-valueerror-in-python\/"},"wordCount":575,"publisher":{"@id":"https:\/\/cloudinary.com\/blog\/#organization"},"image":{"@id":"https:\/\/cloudinary.com\/blog\/questions\/how-to-handle-a-valueerror-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1762598817\/how_to_handle_a_value_error_in_python_featured_image\/how_to_handle_a_value_error_in_python_featured_image.jpg?_i=AA","keywords":["Questions"],"inLanguage":"en-US","copyrightYear":"2025","copyrightHolder":{"@id":"https:\/\/cloudinary.com\/#organization"}},{"@type":"WebPage","@id":"https:\/\/cloudinary.com\/blog\/questions\/how-to-handle-a-valueerror-in-python\/","url":"https:\/\/cloudinary.com\/blog\/questions\/how-to-handle-a-valueerror-in-python\/","name":"How to handle a ValueError in Python","isPartOf":{"@id":"https:\/\/cloudinary.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/cloudinary.com\/blog\/questions\/how-to-handle-a-valueerror-in-python\/#primaryimage"},"image":{"@id":"https:\/\/cloudinary.com\/blog\/questions\/how-to-handle-a-valueerror-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1762598817\/how_to_handle_a_value_error_in_python_featured_image\/how_to_handle_a_value_error_in_python_featured_image.jpg?_i=AA","datePublished":"2025-11-08T10:47:16+00:00","dateModified":"2025-11-08T10:47:17+00:00","description":"ValueError is one of those Python exceptions you will encounter early and often. It usually pops up when a function receives the right type of argument","breadcrumb":{"@id":"https:\/\/cloudinary.com\/blog\/questions\/how-to-handle-a-valueerror-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/cloudinary.com\/blog\/questions\/how-to-handle-a-valueerror-in-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cloudinary.com\/blog\/questions\/how-to-handle-a-valueerror-in-python\/#primaryimage","url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1762598817\/how_to_handle_a_value_error_in_python_featured_image\/how_to_handle_a_value_error_in_python_featured_image.jpg?_i=AA","contentUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1762598817\/how_to_handle_a_value_error_in_python_featured_image\/how_to_handle_a_value_error_in_python_featured_image.jpg?_i=AA","width":2000,"height":1100},{"@type":"BreadcrumbList","@id":"https:\/\/cloudinary.com\/blog\/questions\/how-to-handle-a-valueerror-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/cloudinary.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to handle a ValueError in Python"}]},{"@type":"WebSite","@id":"https:\/\/cloudinary.com\/blog\/#website","url":"https:\/\/cloudinary.com\/blog\/","name":"Cloudinary Blog","description":"","publisher":{"@id":"https:\/\/cloudinary.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/cloudinary.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/cloudinary.com\/blog\/#organization","name":"Cloudinary Blog","url":"https:\/\/cloudinary.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA","contentUrl":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1649718331\/Web_Assets\/blog\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877\/cloudinary_logo_for_white_bg_1937437aa7_19374666c7_193742f877.png?_i=AA","width":312,"height":60,"caption":"Cloudinary Blog"},"image":{"@id":"https:\/\/cloudinary.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/cloudinary.com\/blog\/#\/schema\/person\/43592e43c12520a1e867d456b1e8cf7e","name":"damjanantevski","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cloudinary.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/3b40c995531fe4d510212a06c9d4fc666d2cb8efbfebc98a94191701accf4817?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3b40c995531fe4d510212a06c9d4fc666d2cb8efbfebc98a94191701accf4817?s=96&d=mm&r=g","caption":"damjanantevski"}}]}},"jetpack_featured_media_url":"https:\/\/res.cloudinary.com\/cloudinary-marketing\/images\/f_auto,q_auto\/v1762598817\/how_to_handle_a_value_error_in_python_featured_image\/how_to_handle_a_value_error_in_python_featured_image.jpg?_i=AA","_links":{"self":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts\/39199","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/users\/88"}],"replies":[{"embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/comments?post=39199"}],"version-history":[{"count":1,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts\/39199\/revisions"}],"predecessor-version":[{"id":39201,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/posts\/39199\/revisions\/39201"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/media\/39200"}],"wp:attachment":[{"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/media?parent=39199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/categories?post=39199"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cloudinary.com\/blog\/wp-json\/wp\/v2\/tags?post=39199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}