This depends heavily on your problem, so I will assume you are using a primal formulation. In this case, Neumann conditions are natural, which means that they arise naturally in your problem after you integrate by parts. Again, if this is the case, say your solution is a scalar field $u$ which must verify $\partial_x u = g\quad \in \partial \Omega$. Then, somewhere in your problem you will find a term that looks (maybe) like
$$ \int v \nabla u \cdot \nu, $$
where $\nu$ is you facet normal. Then, your code should have something like this:
n = FacetNormal(mesh)
boundary_gradient = as_vector(g, u.dx(1))
bilinear_form += v * dot(boundary_gradient , n) * dx
If it shows some strage errors in the partial derivative (I have had this problem sometimes), you can use
u_x = dot(grad (u), Constant([1.,0.]))
Let me know if it works. Best regards!