﻿# Ray Serve FastAPI factories: build nonserializable instrumentation inside each replica

Distinguish Ray Serve object-based ingress from a replica-initialized FastAPI builder when middleware or telemetry introduces components that cannot be serialized.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-ray-serve-fastapi-factory-replica-local-instrumentation/
Language: en

## Telemetry changes where application construction must happen

A FastAPI application works locally, then fails while being packaged for Ray Serve after telemetry is added. The issue can be a nonserializable object captured inside the constructed application, rather than an invalid route or inference model. Moving the construction boundary is a different remedy from removing monitoring or changing GPU resources. First identify which object is serialized and which initialization should instead happen inside the replica that will own it.

## Object ingress and builder ingress carry different objects

The [Serve HTTP guide](https://docs.ray.io/en/latest/serve/http-guide.html) describes the factory pattern for applications containing components such as thread locks. In the object-based form, the app must be serializable. In the builder form, a callable constructs the app on each replica. The callable and its captured environment still need an appropriate deployment boundary; moving an already-created unpicklable object into a closure does not magically remove that object from what must be transported.

## Replica initialization is the documented builder boundary

The [ingress API](https://docs.ray.io/en/latest/serve/api/doc/ray.serve.ingress.html) explains that the builder is evaluated during replica initialization. Construct replica-owned clients and middleware in that intended scope. Handlers declared inside a factory do not automatically gain access to a deployment instance’s attributes; explicitly arrange the dependencies they need. The [startup resource guide](/journal/cpu-gpu-hybrid-architecture/) supplies background on separating host-side orchestration from GPU work, but does not replace application-level ownership decisions.

## Instrumentation must be installed consistently

The [OpenTelemetry FastAPI instrumentation documentation](https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/fastapi/fastapi.html) describes application instrumentation interfaces. Confirm the installed dependency set and initialization behavior in a fresh replica, including any exporters and configuration it requires. Avoid applying instrumentation twice through both global startup and the factory. Verify route responses and emitted telemetry separately: a successful HTTP request can coexist with missing traces, while emitted traces do not prove the inference response is correct.

## Reproduce construction on a clean replica

Test a newly started replica rather than only a long-lived development process with preinitialized imports. Record the factory code, dependency versions and application configuration alongside the release identity. Use [artifact verification](/journal/artifact-signature-verification/) to connect that identity to the tested deployment package. The desired evidence is repeatable construction with correct routes and observability; the factory pattern itself is not a guarantee that any captured object, external client or middleware lifecycle will be safe automatically.
