본문 바로가기
프로젝트/나무위키LLM

[ERROR] FastAPI - 422 Unprocessable Entity

by 행복한라이언 2024. 1. 29.
728x90
반응형

1. 에러 발생 및 해결

  • 발생: API테스트(novelist -> librarian -> wiseman 연결 파악)을 위해서 포스트맨에서 POST 요청을 했다. 그런데 다음과 같은 에러가 발생했다.

  • 배경지식: 422 Unprocessable Content ( https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/422 )
    • The HyperText Transfer Protocol (HTTP) 422 Unprocessable Content response status code indicates that the server understands the content type of the request entity, and the syntax of the request entity is correct, but it was unable to process the contained instructions.
    • (GPT) "HTTP 422 Unprocessable Content" 응답 상태 코드는 서버가 요청 엔터티의 콘텐츠 유형을 이해하고 요청 엔터티의 구문이 올바르지만, 포함된 지시사항을 처리할 수 없는 경우를 나타냅니다.
      이는 클라이언트가 올바른 요청을 보냈지만, 서버가 해당 요청을 처리할 수 없다는 것을 의미합니다. 즉, 서버가 요청을 이해할 수 있고 구문상의 문제는 없지만, 요청에 대한 처리가 불가능하다는 것을 의미합니다.
      이 응답 코드는 클라이언트에게 요청 엔터티의 문제를 해결하도록 요청하거나, 서버 측에서 문제를 해결하여 요청을 처리할 수 있도록 하는 데 사용될 수 있습니다. 
  • 상황1: 포스트맨의 에러메세지를 보면 "https://docs.pydantic.dev/2.5/errors/validation_errors/#missing"를 보아 pydantic 사용에 문제가 있는 것 같아보이지만 pydantic을 사용하지 않았다는거..
  • 상황2: 현재는 Form을 사용해서 form-data로 처리되어야한다는 것만 규정하고 있다.
@novelist_router.post("/text")
async def get_resposne(query: Annotated[str, Form()]) -> dict:
....

 

  • 해결: 크나큰 실수를 했다. 변수명을 text에서 query로 바꾸는 중이었는데 get_response의 key도 text에서 query로 변경했었다. 포스트맨에서 text에서 query로 변경하니 잘 작동했다.

2. 참고사이트

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/422

 

422 Unprocessable Content - HTTP | MDN

The HyperText Transfer Protocol (HTTP) 422 Unprocessable Content response status code indicates that the server understands the content type of the request entity, and the syntax of the request entity is correct, but it was unable to process the contained

developer.mozilla.org

 

728x90
반응형