title: FastAPI - Pydantic - Value Error Raises Internal Server Error
body: <p>I am using FastAPI with Pydantic.</p>
  <p>My problem - I need to raise ValueError using Pydantic</p>
  <pre class="lang-py prettyprint-override"><code>from fastapi import FastAPI
  from pydantic import BaseModel, validator
  from fastapi import Depends, HTTPException
  
  app = FastAPI()
  
  class RankInput(BaseModel):
  
      rank: int
  
      @validator('rank')
      def check_if_value_in_range(cls, v):
          &quot;&quot;&quot;
          check if input rank is within range
          &quot;&quot;&quot;
          if not 0 &lt; v &lt; 1000001:
  
              raise ValueError(&quot;Rank Value Must be within range (0,1000000)&quot;)
              #raise HTTPException(status_code=400, detail=&quot;Rank Value Error&quot;) - this works But I am looking for a solution using ValueError
          return v
  
  def get_info_by_rank(rank):
      return rank
  
  @app.get('/rank/{rank}')
  async def get_rank(value: RankInput = Depends()):
      result = get_info_by_rank(value.rank)
      return result
  </code></pre>
  <p>this piece of code gives <code>Internal Server Error</code> when a ValueError is raised</p>
  <pre><code>INFO:     127.0.0.1:59427 - &quot;GET /info/?rank=-1 HTTP/1.1&quot; 500 Internal Server Error
  ERROR:    Exception in ASGI application
  Traceback (most recent call last):
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/uvicorn/protocols/http/h11_impl.py&quot;, line 396, in run_asgi
      result = await app(self.scope, self.receive, self.send)
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/uvicorn/middleware/proxy_headers.py&quot;, line 45, in __call__
      return await self.app(scope, receive, send)
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/fastapi/applications.py&quot;, line 199, in __call__
      await super().__call__(scope, receive, send)
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/applications.py&quot;, line 111, in __call__
      await self.middleware_stack(scope, receive, send)
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/middleware/errors.py&quot;, line 181, in __call__
      raise exc from None
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/middleware/errors.py&quot;, line 159, in __call__
      await self.app(scope, receive, _send)
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/exceptions.py&quot;, line 82, in __call__
      raise exc from None
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/exceptions.py&quot;, line 71, in __call__
      await self.app(scope, receive, sender)
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/routing.py&quot;, line 566, in __call__
      await route.handle(scope, receive, send)
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/routing.py&quot;, line 227, in handle
      await self.app(scope, receive, send)
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/routing.py&quot;, line 41, in app
      response = await func(request)
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/fastapi/routing.py&quot;, line 195, in app
      dependency_overrides_provider=dependency_overrides_provider,
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/fastapi/dependencies/utils.py&quot;, line 550, in solve_dependencies
      solved = await run_in_threadpool(call, **sub_values)
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/concurrency.py&quot;, line 34, in run_in_threadpool
      return await loop.run_in_executor(None, func, *args)
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/thread.py&quot;, line 57, in run
      result = self.fn(*self.args, **self.kwargs)
    File &quot;pydantic/main.py&quot;, line 400, in pydantic.main.BaseModel.__init__
  pydantic.error_wrappers.ValidationError: 1 validation error for GetInput
  rank
    ValueError() takes no keyword arguments (type=type_error)
  ERROR:uvicorn.error:Exception in ASGI application
  Traceback (most recent call last):
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/uvicorn/protocols/http/h11_impl.py&quot;, line 396, in run_asgi
      result = await app(self.scope, self.receive, self.send)
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/uvicorn/middleware/proxy_headers.py&quot;, line 45, in __call__
      return await self.app(scope, receive, send)
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/fastapi/applications.py&quot;, line 199, in __call__
      await super().__call__(scope, receive, send)
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/applications.py&quot;, line 111, in __call__
      await self.middleware_stack(scope, receive, send)
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/middleware/errors.py&quot;, line 181, in __call__
      raise exc from None
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/middleware/errors.py&quot;, line 159, in __call__
      await self.app(scope, receive, _send)
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/exceptions.py&quot;, line 82, in __call__
      raise exc from None
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/exceptions.py&quot;, line 71, in __call__
      await self.app(scope, receive, sender)
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/routing.py&quot;, line 566, in __call__
      await route.handle(scope, receive, send)
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/routing.py&quot;, line 227, in handle
      await self.app(scope, receive, send)
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/routing.py&quot;, line 41, in app
      response = await func(request)
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/fastapi/routing.py&quot;, line 195, in app
      dependency_overrides_provider=dependency_overrides_provider,
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/fastapi/dependencies/utils.py&quot;, line 550, in solve_dependencies
      solved = await run_in_threadpool(call, **sub_values)
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/starlette/concurrency.py&quot;, line 34, in run_in_threadpool
      return await loop.run_in_executor(None, func, *args)
    File &quot;/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/thread.py&quot;, line 57, in run
      result = self.fn(*self.args, **self.kwargs)
    File &quot;pydantic/main.py&quot;, line 400, in pydantic.main.BaseModel.__init__
  pydantic.error_wrappers.ValidationError: 1 validation error for GetInput
  rank
    ValueError() takes no keyword arguments (type=type_error)
  </code></pre>
  <p>I also checked <a href="https://github.com/tiangolo/fastapi/issues/2180" rel="noreferrer">https://github.com/tiangolo/fastapi/issues/2180</a>.</p>
  <p>But I was not able to figure out a solution.</p>
  <p>What I need to do is Raise <code>ValueError</code> with a Custom Status Code.</p>
  <p>Note - I know I can get the Job Done by raising <code>HTTPException</code>.</p>
  <p>But I am looking for a solution using <code>ValueError</code></p>
  <p>Could you tell me where I am going wrong?</p>
  <p>Have Also Posted this Issue on Github - <a href="https://github.com/tiangolo/fastapi/issues/3761" rel="noreferrer">https://github.com/tiangolo/fastapi/issues/3761</a></p>