Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: wrong content-type in POST payload and FastAPI #31

Open
alexanderdann opened this issue Dec 11, 2024 · 3 comments
Open

bug: wrong content-type in POST payload and FastAPI #31

alexanderdann opened this issue Dec 11, 2024 · 3 comments
Assignees
Labels
bug Something isn't working documentation Improvements or additions to documentation

Comments

@alexanderdann
Copy link

Provide environment information

System Information:
System: Darwin
Release: 24.1.0
Machine: arm64
Processor: arm
Python: 3.12.7 (main, Oct 1 2024, 02:05:46) [Clang 16.0.0 (clang-1600.0.26.3)]
encord-agents version: v0.1.2

Describe the bug

I am working on integrating a custom editor agent and started with a minimal example along the lines of your documentation. However it is not working as expected. I get "POST / HTTP/1.1" 422 Unprocessable Entity. It seems as if the payload is not in the expected format.

Chrome tells me that the content-type is:

content-type: text/plain

However for pydantic it needs to be json.

import json
from typing import Annotated

from encord.objects.ontology_labels_impl import LabelRowV2
from encord_agents import FrameData
from encord_agents.fastapi import dep_label_row
from fastapi import Depends, FastAPI, Form, Request
from fastapi.middleware.cors import CORSMiddleware

app = FastAPI()
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*", "https://app.encord.com"],
    allow_methods=["POST"],  # I had to add this line
)


# This does not work, from: https://agents-docs.encord.com/editor_agents/fastapi/
@app.post("/")
def my_agent(
    frame_data: Annotated[FrameData, Form()],
    label_row: Annotated[LabelRowV2, Depends(dep_label_row)],
):
    # ... Do your edits to the labels
    label_row.save()


# This works, mind to uncomment the previous function to see this function at work
@app.post("/")
async def my_agent(
    request: Request,
):
    # ... Do your edits to the labels
    body = await request.body()
    data = json.loads(body)
    frame_data = FrameData(**data)

    return frame_data

To reproduce

pip install encord==0.1.152 "fastapi[standard]==0.115.6" encord-agents==0.1.2 && fastapi dev main.py

Additional information

No response

@frederik-encord frederik-encord added bug Something isn't working documentation Improvements or additions to documentation labels Dec 17, 2024
@frederik-encord frederik-encord self-assigned this Dec 17, 2024
@frederik-encord
Copy link
Collaborator

You are right. Will be fixed with next release.
Could you confirm if this enough? I believe it should be.

@app.post("/")
async def my_agent(
   frame_data: FrameData,
):
    return frame_data

@frederik-encord
Copy link
Collaborator

FYI #36 includes a fix for this issue. Will be included in the next release.

In fact, it'll become even easier to set up a FastAPI server with the right CORS middleware:

No need to set the allow_methods param and the allowed origins. That's all predefined in the EncordCORSMiddleware:

from encord_agents import FrameData
from encord_agents.fastapi.cors import EncordCORSMiddleware

# Initialize FastAPI app
app = FastAPI()
app.add_middleware(EncordCORSMiddleware)

@app.post("/your-endpoint")
def your_endpoint(frame_data: FrameData) -> None:
    ...

Caution

If you find it not working after updating to encord-agents >= 0.1.4 plus using the above template, please check the network tab in your browser to see if triggering your agent sends data with the text/plain content type. If that's the case, it is because we (at Encord) registered that you were using editor agents prior to making the change to application/json content type. As a consequence, we have done our best to keep your existing agents from breaking. Please reach out and we'll upgrade your organisation to application/json.

@alexanderdann
Copy link
Author

Unfortunately I still get the same error. Testing via curl works, i.e.,

curl localhost:8000 -H "Content-type: application/json" -d '{
    "projectHash": "<project_hash>",
    "dataHash": "<data_hash>",
    "frame": 0
}'

This indicates that the message sent from Encord itself is malformed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Improvements or additions to documentation
Projects
None yet
Development

When branches are created from issues, their pull requests are automatically linked.

2 participants