Skip to content

Commit

Permalink
fix(agents-api): codec route fixed + handled explicit checks for code…
Browse files Browse the repository at this point in the history
… decoding (#809)

<!-- ELLIPSIS_HIDDEN -->


> [!IMPORTANT]
> Improves payload decoding and routing in agents-api with enhanced
error handling and compatibility checks.
> 
>   - **Behavior**:
> - Enhanced `decode_payloads()` in `internal/router.py` to handle
Pydantic models, bytes, and metadata more robustly.
> - Added error handling for payload decoding failures, logging errors
in `internal/router.py`.
>   - **Codec**:
> - Updated `from_payload()` in `codec.py` to include explicit checks
for encoding and Python version compatibility.
>     - Added logging for decoding failures in `codec.py`.
>   - **Routing**:
> - Added `agents-api-internal` route in `traefik.yml.template` for
`/api/temporal/{path:.*}` with higher priority than general route.
> 
> <sup>This description was created by </sup>[<img alt="Ellipsis"
src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=julep-ai%2Fjulep&utm_source=github&utm_medium=referral)<sup>
for 75e2363. It will automatically
update as commits are pushed.</sup>


<!-- ELLIPSIS_HIDDEN -->

---------

Co-authored-by: standard-input[bot] <183254018+standard-input[bot]@users.noreply.github.com>
Co-authored-by: Vedantsahai18 <[email protected]>
  • Loading branch information
3 people authored Nov 6, 2024
1 parent 6573688 commit 325145f
Show file tree
Hide file tree
Showing 5 changed files with 283 additions and 222 deletions.
1 change: 1 addition & 0 deletions agents-api/agents_api/routers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from .agents import router as agents_router
from .docs import router as docs_router
from .internal import router as internal_router
from .jobs import router as jobs_router
from .sessions import router as sessions_router
from .tasks import router as tasks_router
Expand Down
31 changes: 28 additions & 3 deletions agents-api/agents_api/routers/internal/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,43 @@

# Decode route
@router.post("/temporal/decode", tags=["temporal"])
async def decode_payloads(req: Request) -> dict:
async def decode_payloads(req: Request):
"""Decodes a list of payloads from the request body.
Args:
req (Request): The request containing the payloads to decode.
Returns:
dict: A dictionary containing the decoded payloads, including any errors encountered.
"""
body = json_format.Parse(await req.body(), Payloads())
payloads = body.payloads

decoded_payloads = []

for p in payloads:
# Attempt to decode the payload
data = converter.from_payload(p)

# Handle Pydantic models
if hasattr(data, "model_dump"):
data = data.model_dump()

decoded_payloads.append({"data": data, "metadata": p.metadata})
# Handle bytes data
elif isinstance(data, bytes):
try:
data = data.decode("utf-8")
except UnicodeDecodeError:
# If it's not UTF-8 encoded text, leave it as bytes
data = str(data)

# Convert metadata more safely
metadata = {}
for k, v in p.metadata.items():
try:
metadata[k] = v.decode() if isinstance(v, bytes) else v
except UnicodeDecodeError:
metadata[k] = str(v)

decoded_payloads.append({"data": data, "metadata": metadata})

return {"payloads": decoded_payloads}
17 changes: 14 additions & 3 deletions agents-api/agents_api/worker/codec.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,21 @@ def from_payload(self, payload: Payload, type_hint: Optional[Type] = None) -> An
f"{sys.version_info.major}.{sys.version_info.minor}".encode()
)

assert payload.metadata["encoding"] == self.b_encoding
assert payload.metadata["python_version"] == current_python_version
# Check if this is a payload we can handle
if (
"encoding" not in payload.metadata
or payload.metadata["encoding"] != self.b_encoding
or "python_version" not in payload.metadata
or payload.metadata["python_version"] != current_python_version
):
# Return the payload data as-is if we can't handle it
return payload.data

return from_payload_data(payload.data, type_hint)
try:
return from_payload_data(payload.data, type_hint)
except Exception as e:
logging.warning(f"Failed to decode payload with our encoder: {e}")
return None


class PydanticPayloadConverter(CompositePayloadConverter):
Expand Down
Loading

0 comments on commit 325145f

Please sign in to comment.